[Mesa-dev] [PATCH 1/8] util: Add more query methods to u_format_parse.Format

Richard Sandiford rsandifo at linux.vnet.ibm.com
Tue Jun 11 07:17:46 PDT 2013


The main aim is to reduce the number of places that access channels[0],
swizzles[0] and swizzles[1] directly.

There is no change to the generated u_format_table.c.

Signed-off-by: Richard Sandiford <rsandifo at linux.vnet.ibm.com>
---
 src/gallium/auxiliary/util/u_format_pack.py  | 27 ++-------------
 src/gallium/auxiliary/util/u_format_parse.py | 50 ++++++++++++++++++++++++----
 src/gallium/auxiliary/util/u_format_table.py | 10 +++---
 3 files changed, 51 insertions(+), 36 deletions(-)

diff --git a/src/gallium/auxiliary/util/u_format_pack.py b/src/gallium/auxiliary/util/u_format_pack.py
index 565d059..c0539b1 100644
--- a/src/gallium/auxiliary/util/u_format_pack.py
+++ b/src/gallium/auxiliary/util/u_format_pack.py
@@ -126,27 +126,6 @@ def is_format_supported(format):
 
     return True
 
-def is_format_pure_unsigned(format):
-    for i in range(4):
-        channel = format.channels[i]
-        if channel.type not in (VOID, UNSIGNED):
-            return False
-        if channel.type == UNSIGNED and channel.pure == False:
-            return False
-
-    return True
-
-
-def is_format_pure_signed(format):
-    for i in range(4):
-        channel = format.channels[i]
-        if channel.type not in (VOID, SIGNED):
-            return False
-        if channel.type == SIGNED and channel.pure == False:
-            return False
-
-    return True
-
 def native_type(format):
     '''Get the native appropriate for a format.'''
 
@@ -161,7 +140,7 @@ def native_type(format):
             return 'uint%u_t' % format.block_size()
         else:
             # For array pixel formats return the integer type that matches the color channel
-            channel = format.channels[0]
+            channel = format.array_element()
             if channel.type in (UNSIGNED, VOID):
                 return 'uint%u_t' % channel.size
             elif channel.type in (SIGNED, FIXED):
@@ -679,7 +658,7 @@ def generate(formats):
             if is_format_supported(format):
                 generate_format_type(format)
 
-            if is_format_pure_unsigned(format):
+            if format.is_pure_unsigned():
                 native_type = 'unsigned'
                 suffix = 'unsigned'
                 channel = Channel(UNSIGNED, False, True, 32)
@@ -693,7 +672,7 @@ def generate(formats):
                 suffix = 'signed'
                 generate_format_unpack(format, channel, native_type, suffix)
                 generate_format_pack(format, channel, native_type, suffix)   
-            elif is_format_pure_signed(format):
+            elif format.is_pure_signed():
                 native_type = 'int'
                 suffix = 'signed'
                 channel = Channel(SIGNED, False, True, 32)
diff --git a/src/gallium/auxiliary/util/u_format_parse.py b/src/gallium/auxiliary/util/u_format_parse.py
index 07052b9..df92005 100755
--- a/src/gallium/auxiliary/util/u_format_parse.py
+++ b/src/gallium/auxiliary/util/u_format_parse.py
@@ -140,23 +140,26 @@ class Format:
                 nr_channels += 1
         return nr_channels
 
-    def is_array(self):
+    def array_element(self):
         if self.layout != PLAIN:
-            return False
+            return None
         ref_channel = self.channels[0]
         if ref_channel.type == VOID:
            ref_channel = self.channels[1]
         for channel in self.channels:
             if channel.size and (channel.size != ref_channel.size or channel.size % 8):
-                return False
+                return None
             if channel.type != VOID:
                 if channel.type != ref_channel.type:
-                    return False
+                    return None
                 if channel.norm != ref_channel.norm:
-                    return False
+                    return None
                 if channel.pure != ref_channel.pure:
-                    return False
-        return True
+                    return None
+        return ref_channel
+
+    def is_array(self):
+        return self.array_element() != None
 
     def is_mixed(self):
         if self.layout != PLAIN:
@@ -203,6 +206,39 @@ class Format:
                 return False
         return True
 
+    def is_pure_color(self):
+        if self.layout != PLAIN or self.colorspace == ZS:
+            return False
+        pures = [channel.pure
+                 for channel in self.channels
+                 if channel.type != VOID]
+        for x in pures:
+           assert x == pures[0]
+        return pures[0]
+
+    def channel_type(self):
+        types = [channel.type
+                 for channel in self.channels
+                 if channel.type != VOID]
+        for x in types:
+           assert x == types[0]
+        return types[0]
+
+    def is_pure_signed(self):
+        return self.is_pure_color() and self.channel_type() == SIGNED
+
+    def is_pure_unsigned(self):
+        return self.is_pure_color() and self.channel_type() == UNSIGNED
+
+    def has_channel(self, id):
+        return self.swizzles[id] != SWIZZLE_NONE
+
+    def has_depth(self):
+        return self.colorspace == ZS and self.has_channel(0)
+
+    def has_stencil(self):
+        return self.colorspace == ZS and self.has_channel(1)
+
     def inv_swizzles(self):
         '''Return an array[4] of inverse swizzle terms'''
         '''Only pick the first matching value to avoid l8 getting blue and i8 getting alpha'''
diff --git a/src/gallium/auxiliary/util/u_format_table.py b/src/gallium/auxiliary/util/u_format_table.py
index 8edb505..efc7f33 100755
--- a/src/gallium/auxiliary/util/u_format_table.py
+++ b/src/gallium/auxiliary/util/u_format_table.py
@@ -132,7 +132,7 @@ def write_format_table(formats):
             print "      %s%s\t/* %s */" % (swizzle_map[swizzle], sep, comment)
         print "   },"
         print "   %s," % (colorspace_map(format.colorspace),)
-        if format.colorspace != ZS and format.channels[0].pure == False:
+        if format.colorspace != ZS and not format.is_pure_color():
             print "   &util_format_%s_unpack_rgba_8unorm," % format.short_name() 
             print "   &util_format_%s_pack_rgba_8unorm," % format.short_name() 
             if format.layout == 's3tc' or format.layout == 'rgtc':
@@ -149,7 +149,7 @@ def write_format_table(formats):
             print "   NULL, /* unpack_rgba_float */" 
             print "   NULL, /* pack_rgba_float */" 
             print "   NULL, /* fetch_rgba_float */" 
-        if format.colorspace == ZS and format.swizzles[0] != SWIZZLE_NONE:
+        if format.has_depth():
             print "   &util_format_%s_unpack_z_32unorm," % format.short_name() 
             print "   &util_format_%s_pack_z_32unorm," % format.short_name() 
             print "   &util_format_%s_unpack_z_float," % format.short_name() 
@@ -159,20 +159,20 @@ def write_format_table(formats):
             print "   NULL, /* pack_z_32unorm */" 
             print "   NULL, /* unpack_z_float */" 
             print "   NULL, /* pack_z_float */" 
-        if format.colorspace == ZS and format.swizzles[1] != SWIZZLE_NONE:
+        if format.has_stencil():
             print "   &util_format_%s_unpack_s_8uint," % format.short_name() 
             print "   &util_format_%s_pack_s_8uint," % format.short_name() 
         else:
             print "   NULL, /* unpack_s_8uint */" 
             print "   NULL, /* pack_s_8uint */"
-        if format.colorspace != ZS and format.channels[0].pure == True and format.channels[0].type == UNSIGNED:
+        if format.is_pure_unsigned():
             print "   &util_format_%s_unpack_unsigned, /* unpack_rgba_uint */" % format.short_name() 
             print "   &util_format_%s_pack_unsigned, /* pack_rgba_uint */" % format.short_name()
             print "   &util_format_%s_unpack_signed, /* unpack_rgba_sint */" % format.short_name()
             print "   &util_format_%s_pack_signed,  /* pack_rgba_sint */" % format.short_name()
             print "   &util_format_%s_fetch_unsigned,  /* fetch_rgba_uint */" % format.short_name()
             print "   NULL  /* fetch_rgba_sint */"
-        elif format.colorspace != ZS and format.channels[0].pure == True and format.channels[0].type == SIGNED:
+        elif format.is_pure_signed():
             print "   &util_format_%s_unpack_unsigned, /* unpack_rgba_uint */" % format.short_name()
             print "   &util_format_%s_pack_unsigned, /* pack_rgba_uint */" % format.short_name()
             print "   &util_format_%s_unpack_signed, /* unpack_rgba_sint */" % format.short_name()
-- 
1.7.11.7



More information about the mesa-dev mailing list