[Mesa-dev] [PATCH 07/29] mesa: Add helper to convert a GL format and type to a mesa (array) format.
Jason Ekstrand
jason at jlekstrand.net
Wed Nov 19 12:11:43 PST 2014
General comment: Maybe this would be better in gltypes rather than in
mesa_formats
On Tue, Nov 18, 2014 at 1:23 AM, Iago Toral Quiroga <itoral at igalia.com>
wrote:
> ---
> src/mesa/main/formats.c | 285
> ++++++++++++++++++++++++++++++++++++++++++++++++
> src/mesa/main/formats.h | 3 +
> 2 files changed, 288 insertions(+)
>
> diff --git a/src/mesa/main/formats.c b/src/mesa/main/formats.c
> index 06e8973..7464d89 100644
> --- a/src/mesa/main/formats.c
> +++ b/src/mesa/main/formats.c
> @@ -325,6 +325,291 @@ _mesa_format_from_array_format(uint32_t array_format)
> return MESA_FORMAT_NONE;
> }
>
> +static 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;
> +}
> +
> +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;
> + case GL_UNSIGNED_INT_8_8_8_8:
> + case GL_UNSIGNED_INT_8_8_8_8_REV:
> + array_format.type = MESA_ARRAY_FORMAT_TYPE_UBYTE;
>
If you put these in the GL type switch below as returning the
MESA_FORMAT_R8G8B8A8 or whatever, then the code in
mesa_format_get_array_format will fix up the swizzling for you.
> + 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;
> +
> + /* Check if we need to swap the swizzle for
> GL_UNSIGNED_INT_8_8_8_8* */
> + bool swap_swizzle =
> + (type == GL_UNSIGNED_INT_8_8_8_8 && _mesa_little_endian()) ||
> + (type == GL_UNSIGNED_INT_8_8_8_8_REV && !_mesa_little_endian());
> + if (swap_swizzle) {
> + GLubyte swizzle[4], tmp[4];
> + const GLubyte map_3210[6] = { 3, 2, 1, 0, 4, 5 };
> + int i;
> +
> + tmp[0] = array_format.swizzle_x;
> + tmp[1] = array_format.swizzle_y;
> + tmp[2] = array_format.swizzle_z;
> + tmp[3] = array_format.swizzle_w;
> +
> + for (i = 0; i < 4; i++) {
> + if (tmp[i] == MESA_FORMAT_SWIZZLE_NONE)
> + swizzle[i] = MESA_FORMAT_SWIZZLE_NONE;
> + else
> + swizzle[i] = map_3210[tmp[i]];
> + }
> +
> + array_format.swizzle_x = swizzle[0];
> + array_format.swizzle_y = swizzle[1];
> + array_format.swizzle_z = swizzle[2];
> + array_format.swizzle_w = swizzle[3];
> + } else if (!_mesa_little_endian()) {
> + array_format = 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;
>
I mentioned this in a comment on an earlier patch, but I don't think this
combination is a real thing. If we currently allow it, we should
double-check the GL spec and disallow it if we can. That's just a bad
format.
+ 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;
>
Same here
> + break;
> + case GL_UNSIGNED_INT_8_8_8_8:
> + /* Formats other than BGRA are handled as array formats */
> + if (format == GL_BGRA)
> + return MESA_FORMAT_A8R8G8B8_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");
> +}
> +
> /** 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 7a792d4..092a1a2 100644
> --- a/src/mesa/main/formats.h
> +++ b/src/mesa/main/formats.h
> @@ -546,6 +546,9 @@ _mesa_format_to_array_format(mesa_format format);
> extern mesa_format
> _mesa_format_from_array_format(uint32_t array_format);
>
> +extern uint32_t
> +_mesa_format_from_format_and_type(GLenum format, GLenum type);
> +
> extern GLboolean
> _mesa_is_format_compressed(mesa_format format);
>
> --
> 1.9.1
>
> _______________________________________________
> mesa-dev mailing list
> mesa-dev at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/mesa-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/mesa-dev/attachments/20141119/9605aab4/attachment-0001.html>
More information about the mesa-dev
mailing list