[Mesa-dev] [PATCH v2 07/29] mesa: Add helper to convert a GL format and type to a mesa (array) format.

Iago Toral Quiroga itoral at igalia.com
Mon Dec 1 03:05:00 PST 2014


v2 after review by Jason Ekstrand:
- Move _mesa_format_from_format_and_type to glformats
- Return a mesa_format for GL_UNSIGNED_INT_8_8_8_8(_REV)
---
 src/mesa/main/formats.c   |  18 +++-
 src/mesa/main/formats.h   |   7 ++
 src/mesa/main/glformats.c | 259 +++++++++++++++++++++++++++++++++++++++++++++-
 src/mesa/main/glformats.h |   2 +
 4 files changed, 281 insertions(+), 5 deletions(-)

diff --git a/src/mesa/main/formats.c b/src/mesa/main/formats.c
index d0e218c..87f6bf1 100644
--- a/src/mesa/main/formats.c
+++ b/src/mesa/main/formats.c
@@ -333,8 +333,8 @@ _mesa_get_format_swizzle(mesa_format format, uint8_t swizzle_out[4])
    memcpy(swizzle_out, info->Swizzle, sizeof(info->Swizzle));
 }
 
-static mesa_array_format
-array_format_flip_channels(mesa_array_format format)
+mesa_array_format
+_mesa_array_format_flip_channels(mesa_array_format format)
 {
    if (format.num_channels == 1)
       return format;
@@ -366,7 +366,7 @@ _mesa_format_to_array_format(mesa_format format)
    if (_mesa_little_endian())
       return info->ArrayFormat.as_uint;
    else
-      return array_format_flip_channels(info->ArrayFormat).as_uint;
+      return _mesa_array_format_flip_channels(info->ArrayFormat).as_uint;
 }
 
 mesa_format
@@ -378,7 +378,7 @@ _mesa_format_from_array_format(uint32_t array_format)
    af.as_uint = array_format;
    af.pad = 0;
    if (!_mesa_little_endian())
-      af = array_format_flip_channels(af);
+      af = _mesa_array_format_flip_channels(af);
 
    assert(af.array_format_bit);
    for (f = 1; f < MESA_FORMAT_COUNT; ++f)
@@ -388,6 +388,16 @@ _mesa_format_from_array_format(uint32_t array_format)
    return MESA_FORMAT_NONE;
 }
 
+void
+_mesa_array_format_set_swizzle(mesa_array_format *array_format,
+                               int x, int y, int z, int w)
+{
+   array_format->swizzle_x = x;
+   array_format->swizzle_y = y;
+   array_format->swizzle_z = z;
+   array_format->swizzle_w = w;
+}
+
 /** Is the given format a compressed format? */
 GLboolean
 _mesa_is_format_compressed(mesa_format format)
diff --git a/src/mesa/main/formats.h b/src/mesa/main/formats.h
index e0e5804..944c410 100644
--- a/src/mesa/main/formats.h
+++ b/src/mesa/main/formats.h
@@ -537,9 +537,16 @@ _mesa_get_format_base_format(uint32_t format);
 extern void
 _mesa_get_format_block_size(mesa_format format, GLuint *bw, GLuint *bh);
 
+extern mesa_array_format
+_mesa_array_format_flip_channels(mesa_array_format format);
+
 extern void
 _mesa_get_format_swizzle(mesa_format format, uint8_t swizzle_out[4]);
 
+extern void
+_mesa_array_format_set_swizzle(mesa_array_format *array_format,
+                               int x, int y, int z, int w);
+
 extern uint32_t
 _mesa_format_to_array_format(mesa_format format);
 
diff --git a/src/mesa/main/glformats.c b/src/mesa/main/glformats.c
index 00478f9..d3ea749 100644
--- a/src/mesa/main/glformats.c
+++ b/src/mesa/main/glformats.c
@@ -27,7 +27,7 @@
 
 #include "context.h"
 #include "glformats.h"
-
+#include "formats.h"
 
 /**
  * \return GL_TRUE if type is packed pixel type, GL_FALSE otherwise.
@@ -2207,3 +2207,260 @@ _mesa_es3_error_check_format_and_type(GLenum format, GLenum type,
 
    return GL_NO_ERROR;
 }
+
+static bool
+_mesa_array_format_set_swizzle_from_format(mesa_array_format *array_format,
+                                           GLenum format)
+{
+   switch (format) {
+   case GL_RGBA:
+   case GL_RGBA_INTEGER_EXT:
+      _mesa_array_format_set_swizzle(array_format, 0, 1, 2, 3);
+      return true;
+   case GL_BGRA:
+   case GL_BGRA_INTEGER_EXT:
+      _mesa_array_format_set_swizzle(array_format, 2, 1, 0, 3);
+      return true;
+   case GL_ABGR_EXT:
+      _mesa_array_format_set_swizzle(array_format, 3, 2, 1, 0);
+      return true;
+   case GL_RGB:
+   case GL_RGB_INTEGER_EXT:
+      _mesa_array_format_set_swizzle(array_format, 0, 1, 2, 5);
+      return true;
+   case GL_BGR:
+   case GL_BGR_INTEGER_EXT:
+      _mesa_array_format_set_swizzle(array_format, 2, 1, 0, 5);
+      return true;
+   case GL_LUMINANCE_ALPHA:
+   case GL_LUMINANCE_ALPHA_INTEGER_EXT:
+      _mesa_array_format_set_swizzle(array_format, 0, 0, 0, 1);
+      return true;
+   case GL_RG:
+   case GL_RG_INTEGER:
+      _mesa_array_format_set_swizzle(array_format, 0, 1, 4, 5);
+      return true;
+   case GL_RED:
+   case GL_RED_INTEGER_EXT:
+      _mesa_array_format_set_swizzle(array_format, 0, 4, 4, 5);
+      return true;
+   case GL_GREEN:
+   case GL_GREEN_INTEGER_EXT:
+      _mesa_array_format_set_swizzle(array_format, 4, 0, 4, 5);
+      return true;
+   case GL_BLUE:
+   case GL_BLUE_INTEGER_EXT:
+      _mesa_array_format_set_swizzle(array_format, 4, 4, 0, 5);
+      return true;
+   case GL_ALPHA:
+   case GL_ALPHA_INTEGER_EXT:
+      _mesa_array_format_set_swizzle(array_format, 4, 4, 4, 0);
+      return true;
+   case GL_LUMINANCE:
+   case GL_LUMINANCE_INTEGER_EXT:
+      _mesa_array_format_set_swizzle(array_format, 0, 0, 0, 5);
+      return true;
+   case GL_INTENSITY:
+      _mesa_array_format_set_swizzle(array_format, 0, 0, 0, 0);
+      return true;
+   default:
+      return false;
+   }
+}
+
+/**
+* Take an OpenGL format (GL_RGB, GL_RGBA, etc), OpenGL data type (GL_INT,
+* GL_FOAT, etc) and return a matching mesa_array_format or a mesa_format
+* otherwise (for non-array formats).
+*
+* This function will typically be used to compute a mesa format from a GL type
+* so we can then call _mesa_format_convert. This function does
+* not consider byte swapping, so it returns types assuming that no byte
+* swapping is involved. If byte swapping is involved then clients are supposed
+* to handle that on their side before calling _mesa_format_convert.
+*
+* This function returns an uint32_t that can pack a mesa_format or a
+* mesa_array_format. Clients must check the mesa array format bit
+* (MESA_ARRAY_FORMAT_BIT) on the return value to know if the returned
+* format is a mesa_array_format or a mesa_format.
+*/
+uint32_t
+_mesa_format_from_format_and_type(GLenum format, GLenum type)
+{
+   mesa_array_format array_format;
+
+   bool is_array_format = true;
+
+   /* Map the OpenGL data type to an array format data type */
+   switch (type) {
+   case GL_UNSIGNED_BYTE:
+      array_format.type = MESA_ARRAY_FORMAT_TYPE_UBYTE;
+      break;
+   case GL_BYTE:
+      array_format.type = MESA_ARRAY_FORMAT_TYPE_BYTE;
+      break;
+   case GL_UNSIGNED_SHORT:
+      array_format.type = MESA_ARRAY_FORMAT_TYPE_USHORT;
+      break;
+   case GL_SHORT:
+      array_format.type = MESA_ARRAY_FORMAT_TYPE_SHORT;
+      break;
+   case GL_UNSIGNED_INT:
+      array_format.type = MESA_ARRAY_FORMAT_TYPE_UINT;
+      break;
+   case GL_INT:
+      array_format.type = MESA_ARRAY_FORMAT_TYPE_INT;
+      break;
+   case GL_HALF_FLOAT:
+      array_format.type = MESA_ARRAY_FORMAT_TYPE_HALF;
+      break;
+   case GL_FLOAT:
+      array_format.type = MESA_ARRAY_FORMAT_TYPE_FLOAT;
+      break;
+   default:
+      is_array_format = false;
+      break;
+   }
+
+   /* Next we extract array swizzle information from the OpenGL format */
+   if (is_array_format) {
+      is_array_format =
+         _mesa_array_format_set_swizzle_from_format(&array_format, format);
+   }
+
+   /* If this is an array format type after checking data type and format,
+    * fill in the remaining data
+    */
+   if (is_array_format) {
+      array_format.normalized = !_mesa_is_enum_format_integer(format);
+      array_format.num_channels = _mesa_components_in_format(format);
+      array_format.pad = 0;
+      array_format.array_format_bit = 1;
+
+      if (!_mesa_little_endian())
+         array_format = _mesa_array_format_flip_channels(array_format);
+
+      return array_format.as_uint;
+   }
+
+   /* Otherwise this is not an array format, so return the mesa_format
+    * matching the OpenGL format and data type
+    */
+   switch (type) {
+   case GL_UNSIGNED_SHORT_5_6_5:
+     if (format == GL_RGB)
+         return MESA_FORMAT_B5G6R5_UNORM;
+      else if (format == GL_BGR)
+         return MESA_FORMAT_R5G6B5_UNORM;
+      break;
+   case GL_UNSIGNED_SHORT_5_6_5_REV:
+      if (format == GL_RGB)
+         return MESA_FORMAT_R5G6B5_UNORM;
+      else if (format == GL_BGR)
+         return MESA_FORMAT_B5G6R5_UNORM;
+      break;
+   case GL_UNSIGNED_SHORT_4_4_4_4:
+      if (format == GL_RGBA)
+         return MESA_FORMAT_A4B4G4R4_UNORM;
+      else if (format == GL_BGRA)
+         return MESA_FORMAT_A4R4G4B4_UNORM;
+      else if (format == GL_ABGR_EXT)
+         return MESA_FORMAT_R4G4B4A4_UNORM;
+      break;
+   case GL_UNSIGNED_SHORT_4_4_4_4_REV:
+      if (format == GL_RGBA)
+         return MESA_FORMAT_R4G4B4A4_UNORM;
+      else if (format == GL_BGRA)
+         return MESA_FORMAT_B4G4R4A4_UNORM;
+      else if (format == GL_ABGR_EXT)
+         return MESA_FORMAT_A4B4G4R4_UNORM;
+      break;
+   case GL_UNSIGNED_SHORT_5_5_5_1:
+      if (format == GL_RGBA)
+         return MESA_FORMAT_A1B5G5R5_UNORM;
+      else if (format == GL_BGRA)
+         return MESA_FORMAT_A1R5G5B5_UNORM;
+      else if (format == GL_ABGR_EXT)
+         return MESA_FORMAT_R1G5B5A5_UNORM;
+      break;
+   case GL_UNSIGNED_SHORT_1_5_5_5_REV:
+      if (format == GL_RGBA)
+         return MESA_FORMAT_R5G5B5A1_UNORM;
+      else if (format == GL_BGRA)
+         return MESA_FORMAT_B5G5R5A1_UNORM;
+      else if (format == GL_ABGR_EXT)
+         return MESA_FORMAT_A5B5G5R1_UNORM;
+      break;
+   case GL_UNSIGNED_BYTE_3_3_2:
+      if (format == GL_RGB)
+         return MESA_FORMAT_B2G3R3_UNORM;
+      break;
+   case GL_UNSIGNED_BYTE_2_3_3_REV:
+      if (format == GL_RGB)
+         return MESA_FORMAT_R3G3B2_UNORM;
+      break;
+   case GL_UNSIGNED_INT_5_9_9_9_REV:
+      if (format == GL_RGB)
+         return MESA_FORMAT_R9G9B9E5_FLOAT;
+      break;
+   case GL_UNSIGNED_INT_10_10_10_2:
+      if (format == GL_RGBA)
+         return MESA_FORMAT_A2B10G10R10_UNORM;
+      else if (format == GL_RGBA_INTEGER)
+         return MESA_FORMAT_A2B10G10R10_UINT;
+      else if (format == GL_BGRA)
+         return MESA_FORMAT_A2R10G10B10_UNORM;
+      else if (format == GL_BGRA_INTEGER)
+         return MESA_FORMAT_A2R10G10B10_UINT;
+      else if (format == GL_ABGR_EXT)
+         return MESA_FORMAT_R2G10B10A10_UNORM;
+      break;
+   case GL_UNSIGNED_INT_2_10_10_10_REV:
+      if (format == GL_RGBA)
+         return MESA_FORMAT_R10G10B10A2_UNORM;
+      else if (format == GL_RGBA_INTEGER)
+         return MESA_FORMAT_R10G10B10A2_UINT;
+      else if (format == GL_BGRA)
+         return MESA_FORMAT_B10G10R10A2_UNORM;
+      else if (format == GL_BGRA_INTEGER)
+         return MESA_FORMAT_B10G10R10A2_UINT;
+      else if (format == GL_ABGR_EXT)
+         return MESA_FORMAT_A10B10G10R2_UNORM;
+      break;
+   case GL_UNSIGNED_INT_8_8_8_8:
+      if (format == GL_RGBA)
+         return MESA_FORMAT_A8B8G8R8_UNORM;
+      else if (format == GL_BGRA)
+         return MESA_FORMAT_A8R8G8B8_UNORM;
+      else if (format == GL_ABGR_EXT)
+         return MESA_FORMAT_R8G8B8A8_UNORM;
+      break;
+   case GL_UNSIGNED_INT_8_8_8_8_REV:
+      if (format == GL_RGBA)
+         return MESA_FORMAT_R8G8B8A8_UNORM;
+      else if (format == GL_BGRA)
+         return MESA_FORMAT_B8G8R8A8_UNORM;
+      else if (format == GL_ABGR_EXT)
+         return MESA_FORMAT_A8B8G8R8_UNORM;
+      break;
+   case GL_UNSIGNED_SHORT_8_8_MESA:
+      if (format == GL_YCBCR_MESA)
+         return MESA_FORMAT_YCBCR;
+      break;
+   case GL_UNSIGNED_SHORT_8_8_REV_MESA:
+      if (format == GL_YCBCR_MESA)
+         return MESA_FORMAT_YCBCR_REV;
+      break;
+   case GL_UNSIGNED_INT_10F_11F_11F_REV:
+      if (format == GL_RGB)
+         return MESA_FORMAT_R11G11B10_FLOAT;
+   default:
+      break;
+   }
+
+   /* If we got here it means that we could not find a Mesa format that
+    * matches the GL format/type provided. We may need to add a new Mesa
+    * format in that case.
+    */
+   assert(!"Unsupported format");
+}
diff --git a/src/mesa/main/glformats.h b/src/mesa/main/glformats.h
index 7b03215..56f2a5f 100644
--- a/src/mesa/main/glformats.h
+++ b/src/mesa/main/glformats.h
@@ -125,6 +125,8 @@ extern GLenum
 _mesa_es3_error_check_format_and_type(GLenum format, GLenum type,
                                       GLenum internalFormat);
 
+extern uint32_t
+_mesa_format_from_format_and_type(GLenum format, GLenum type);
 
 #ifdef __cplusplus
 }
-- 
1.9.1



More information about the mesa-dev mailing list