[Mesa-dev] [PATCH 07/29] mesa: Add helper to convert a GL format and type to a mesa (array) format.
--- 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; + 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_for
Re: [Mesa-dev] [PATCH 07/29] mesa: Add helper to convert a GL format and type to a mesa (array) format.
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 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 typ
Re: [Mesa-dev] [PATCH 07/29] mesa: Add helper to convert a GL format and type to a mesa (array) format.
On Wed, 2014-11-19 at 12:11 -0800, Jason Ekstrand wrote: > General comment: Maybe this would be better in gltypes rather than in > mesa_formats Ok, I'll move it. > On Tue, Nov 18, 2014 at 1:23 AM, Iago Toral Quiroga > wrote: > --- > src/mesa/main/formats.c | 285 > > src/mesa/main/formats.h | 3 + > 2 files changed, 288 insertions(+) (...) > + > +/** > +* 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. Do you mean returning a mesa_format instead of a mesa_array_format for the *8_8_8_8* types? Sure, we can do that. > > + 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; > + > +