[Mesa-dev] [PATCH 07/12] main/format_utils: Add a function for determining if a format is actually an array format and computing the array format parameters

Jason Ekstrand jason at jlekstrand.net
Thu Jul 17 11:04:29 PDT 2014


This is a direct helper function for using _mesa_swizzle_and_convert

Signed-off-by: Jason Ekstrand <jason.ekstrand at intel.com>
---
 src/mesa/main/format_utils.c | 93 ++++++++++++++++++++++++++++++++++++++++++++
 src/mesa/main/format_utils.h |  4 ++
 2 files changed, 97 insertions(+)

diff --git a/src/mesa/main/format_utils.c b/src/mesa/main/format_utils.c
index 0cb3eae..b9c7a54 100644
--- a/src/mesa/main/format_utils.c
+++ b/src/mesa/main/format_utils.c
@@ -55,6 +55,99 @@ _mesa_srgb_ubyte_to_linear_float(uint8_t cl)
    return lut[cl];
 }
 
+static const uint8_t map_identity[7] = { 0, 1, 2, 3, 4, 5, 6 };
+static const uint8_t map_3210[7] = { 3, 2, 1, 0, 4, 5, 6 };
+static const uint8_t map_1032[7] = { 1, 0, 3, 2, 4, 5, 6 };
+
+/**
+ * A helper function for figuring out if a (possibly packed) format is
+ * actually an array format and how to work with it.  If the format can not
+ * be used as an array format, thus function returns false.
+ */
+bool
+_mesa_format_to_array(mesa_format format, GLenum *type, int *num_components,
+                      uint8_t swizzle[4], bool *normalized)
+{
+   int i;
+   GLuint format_components;
+   uint8_t packed_swizzle[4];
+   const uint8_t *endian;
+
+   if (_mesa_is_format_compressed(format))
+      return false;
+
+   *normalized = !_mesa_is_format_integer(format);
+
+   _mesa_format_to_type_and_comps(format, type, &format_components);
+   switch (_mesa_get_format_layout(format)) {
+   case MESA_FORMAT_LAYOUT_ARRAY:
+      *num_components = format_components;
+      _mesa_get_format_swizzle(format, swizzle);
+      return true;
+   case MESA_FORMAT_LAYOUT_PACKED:
+      switch (*type) {
+      case GL_UNSIGNED_BYTE:
+      case GL_BYTE:
+         if (_mesa_get_format_max_bits(format) != 8)
+            return false;
+         *num_components = _mesa_get_format_bytes(format);
+         switch (*num_components) {
+         case 1:
+            endian = map_identity;
+            break;
+         case 2:
+            endian = _mesa_little_endian() ? map_identity : map_1032;
+            break;
+         case 4:
+            endian = _mesa_little_endian() ? map_identity : map_3210;
+            break;
+         default:
+            assert(!"Invalid number of components");
+         }
+         break;
+      case GL_UNSIGNED_SHORT:
+      case GL_SHORT:
+      case GL_HALF_FLOAT:
+         if (_mesa_get_format_max_bits(format) != 16)
+            return false;
+         *num_components = _mesa_get_format_bytes(format) / 2;
+         switch (*num_components) {
+         case 1:
+            endian = map_identity;
+            break;
+         case 2:
+            endian = _mesa_little_endian() ? map_identity : map_1032;
+            break;
+         default:
+            assert(!"Invalid number of components");
+         }
+         break;
+      case GL_UNSIGNED_INT:
+      case GL_INT:
+      case GL_FLOAT:
+         /* This isn't packed.  At least not really. */
+         assert(format_components == 1);
+         if (_mesa_get_format_max_bits(format) != 32)
+            return false;
+         *num_components = format_components;
+         endian = map_identity;
+         break;
+      default:
+         return false;
+      }
+
+      _mesa_get_format_swizzle(format, packed_swizzle);
+
+      for (i = 0; i < 4; ++i)
+         swizzle[i] = endian[packed_swizzle[i]];
+
+      return true;
+   case MESA_FORMAT_LAYOUT_OTHER:
+   default:
+      return false;
+   }
+}
+
 static bool
 swizzle_convert_try_memcpy(void *dst, GLenum dst_type, int num_dst_channels,
                            const void *src, GLenum src_type, int num_src_channels,
diff --git a/src/mesa/main/format_utils.h b/src/mesa/main/format_utils.h
index c5dab7b..990c3f2 100644
--- a/src/mesa/main/format_utils.h
+++ b/src/mesa/main/format_utils.h
@@ -78,6 +78,10 @@ _mesa_srgb_to_linear(float cs)
 
 float _mesa_srgb_ubyte_to_linear_float(uint8_t cl);
 
+bool
+_mesa_format_to_array(mesa_format, GLenum *type, int *num_components,
+                      uint8_t swizzle[4], bool *normalized);
+
 void
 _mesa_swizzle_and_convert(void *dst, GLenum dst_type, int num_dst_channels,
                           const void *src, GLenum src_type, int num_src_channels,
-- 
2.0.1



More information about the mesa-dev mailing list