Commit a1287f54 authored by Brian Paul's avatar Brian Paul

mesa: move more format helper functions to glformats.c

parent 8b762ebd
......@@ -30,6 +30,7 @@
#include "glheader.h"
#include "mtypes.h"
#include "glformats.h"
struct gl_context;
......
......@@ -51,6 +51,7 @@
#endif
#include "framebuffer.h"
#include "glapi/glapi.h"
#include "glformats.h"
#include "hash.h"
#include "image.h"
#include "light.h"
......
......@@ -38,8 +38,8 @@
#include "fbobject.h"
#include "formats.h"
#include "framebuffer.h"
#include "glformats.h"
#include "hash.h"
#include "image.h"
#include "macros.h"
#include "mfeatures.h"
#include "mtypes.h"
......
......@@ -28,6 +28,285 @@
#include "glformats.h"
/**
* \return GL_TRUE if type is packed pixel type, GL_FALSE otherwise.
*/
GLboolean
_mesa_type_is_packed(GLenum type)
{
switch (type) {
case GL_UNSIGNED_BYTE_3_3_2:
case GL_UNSIGNED_BYTE_2_3_3_REV:
case MESA_UNSIGNED_BYTE_4_4:
case GL_UNSIGNED_SHORT_5_6_5:
case GL_UNSIGNED_SHORT_5_6_5_REV:
case GL_UNSIGNED_SHORT_4_4_4_4:
case GL_UNSIGNED_SHORT_4_4_4_4_REV:
case GL_UNSIGNED_SHORT_5_5_5_1:
case GL_UNSIGNED_SHORT_1_5_5_5_REV:
case GL_UNSIGNED_INT_8_8_8_8:
case GL_UNSIGNED_INT_8_8_8_8_REV:
case GL_UNSIGNED_INT_10_10_10_2:
case GL_UNSIGNED_INT_2_10_10_10_REV:
case GL_UNSIGNED_SHORT_8_8_MESA:
case GL_UNSIGNED_SHORT_8_8_REV_MESA:
case GL_UNSIGNED_INT_24_8_EXT:
case GL_UNSIGNED_INT_5_9_9_9_REV:
case GL_UNSIGNED_INT_10F_11F_11F_REV:
case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
return GL_TRUE;
}
return GL_FALSE;
}
/**
* Get the size of a GL data type.
*
* \param type GL data type.
*
* \return the size, in bytes, of the given data type, 0 if a GL_BITMAP, or -1
* if an invalid type enum.
*/
GLint
_mesa_sizeof_type(GLenum type)
{
switch (type) {
case GL_BITMAP:
return 0;
case GL_UNSIGNED_BYTE:
return sizeof(GLubyte);
case GL_BYTE:
return sizeof(GLbyte);
case GL_UNSIGNED_SHORT:
return sizeof(GLushort);
case GL_SHORT:
return sizeof(GLshort);
case GL_UNSIGNED_INT:
return sizeof(GLuint);
case GL_INT:
return sizeof(GLint);
case GL_FLOAT:
return sizeof(GLfloat);
case GL_DOUBLE:
return sizeof(GLdouble);
case GL_HALF_FLOAT_ARB:
return sizeof(GLhalfARB);
case GL_FIXED:
return sizeof(GLfixed);
default:
return -1;
}
}
/**
* Same as _mesa_sizeof_type() but also accepting the packed pixel
* format data types.
*/
GLint
_mesa_sizeof_packed_type(GLenum type)
{
switch (type) {
case GL_BITMAP:
return 0;
case GL_UNSIGNED_BYTE:
return sizeof(GLubyte);
case GL_BYTE:
return sizeof(GLbyte);
case GL_UNSIGNED_SHORT:
return sizeof(GLushort);
case GL_SHORT:
return sizeof(GLshort);
case GL_UNSIGNED_INT:
return sizeof(GLuint);
case GL_INT:
return sizeof(GLint);
case GL_HALF_FLOAT_ARB:
return sizeof(GLhalfARB);
case GL_FLOAT:
return sizeof(GLfloat);
case GL_UNSIGNED_BYTE_3_3_2:
case GL_UNSIGNED_BYTE_2_3_3_REV:
case MESA_UNSIGNED_BYTE_4_4:
return sizeof(GLubyte);
case GL_UNSIGNED_SHORT_5_6_5:
case GL_UNSIGNED_SHORT_5_6_5_REV:
case GL_UNSIGNED_SHORT_4_4_4_4:
case GL_UNSIGNED_SHORT_4_4_4_4_REV:
case GL_UNSIGNED_SHORT_5_5_5_1:
case GL_UNSIGNED_SHORT_1_5_5_5_REV:
case GL_UNSIGNED_SHORT_8_8_MESA:
case GL_UNSIGNED_SHORT_8_8_REV_MESA:
return sizeof(GLushort);
case GL_UNSIGNED_INT_8_8_8_8:
case GL_UNSIGNED_INT_8_8_8_8_REV:
case GL_UNSIGNED_INT_10_10_10_2:
case GL_UNSIGNED_INT_2_10_10_10_REV:
case GL_UNSIGNED_INT_24_8_EXT:
case GL_UNSIGNED_INT_5_9_9_9_REV:
case GL_UNSIGNED_INT_10F_11F_11F_REV:
return sizeof(GLuint);
case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
return 8;
default:
return -1;
}
}
/**
* Get the number of components in a pixel format.
*
* \param format pixel format.
*
* \return the number of components in the given format, or -1 if a bad format.
*/
GLint
_mesa_components_in_format(GLenum format)
{
switch (format) {
case GL_COLOR_INDEX:
case GL_STENCIL_INDEX:
case GL_DEPTH_COMPONENT:
case GL_RED:
case GL_RED_INTEGER_EXT:
case GL_GREEN:
case GL_GREEN_INTEGER_EXT:
case GL_BLUE:
case GL_BLUE_INTEGER_EXT:
case GL_ALPHA:
case GL_ALPHA_INTEGER_EXT:
case GL_LUMINANCE:
case GL_LUMINANCE_INTEGER_EXT:
case GL_INTENSITY:
return 1;
case GL_LUMINANCE_ALPHA:
case GL_LUMINANCE_ALPHA_INTEGER_EXT:
case GL_RG:
case GL_YCBCR_MESA:
case GL_DEPTH_STENCIL_EXT:
case GL_DUDV_ATI:
case GL_DU8DV8_ATI:
case GL_RG_INTEGER:
return 2;
case GL_RGB:
case GL_BGR:
case GL_RGB_INTEGER_EXT:
case GL_BGR_INTEGER_EXT:
return 3;
case GL_RGBA:
case GL_BGRA:
case GL_ABGR_EXT:
case GL_RGBA_INTEGER_EXT:
case GL_BGRA_INTEGER_EXT:
return 4;
default:
return -1;
}
}
/**
* Get the bytes per pixel of pixel format type pair.
*
* \param format pixel format.
* \param type pixel type.
*
* \return bytes per pixel, or -1 if a bad format or type was given.
*/
GLint
_mesa_bytes_per_pixel(GLenum format, GLenum type)
{
GLint comps = _mesa_components_in_format(format);
if (comps < 0)
return -1;
switch (type) {
case GL_BITMAP:
return 0; /* special case */
case GL_BYTE:
case GL_UNSIGNED_BYTE:
return comps * sizeof(GLubyte);
case GL_SHORT:
case GL_UNSIGNED_SHORT:
return comps * sizeof(GLshort);
case GL_INT:
case GL_UNSIGNED_INT:
return comps * sizeof(GLint);
case GL_FLOAT:
return comps * sizeof(GLfloat);
case GL_HALF_FLOAT_ARB:
return comps * sizeof(GLhalfARB);
case GL_UNSIGNED_BYTE_3_3_2:
case GL_UNSIGNED_BYTE_2_3_3_REV:
if (format == GL_RGB || format == GL_BGR ||
format == GL_RGB_INTEGER_EXT || format == GL_BGR_INTEGER_EXT)
return sizeof(GLubyte);
else
return -1; /* error */
case GL_UNSIGNED_SHORT_5_6_5:
case GL_UNSIGNED_SHORT_5_6_5_REV:
if (format == GL_RGB || format == GL_BGR ||
format == GL_RGB_INTEGER_EXT || format == GL_BGR_INTEGER_EXT)
return sizeof(GLushort);
else
return -1; /* error */
case GL_UNSIGNED_SHORT_4_4_4_4:
case GL_UNSIGNED_SHORT_4_4_4_4_REV:
case GL_UNSIGNED_SHORT_5_5_5_1:
case GL_UNSIGNED_SHORT_1_5_5_5_REV:
if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT ||
format == GL_RGBA_INTEGER_EXT || format == GL_BGRA_INTEGER_EXT)
return sizeof(GLushort);
else
return -1;
case GL_UNSIGNED_INT_8_8_8_8:
case GL_UNSIGNED_INT_8_8_8_8_REV:
case GL_UNSIGNED_INT_10_10_10_2:
case GL_UNSIGNED_INT_2_10_10_10_REV:
if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT ||
format == GL_RGBA_INTEGER_EXT || format == GL_BGRA_INTEGER_EXT)
return sizeof(GLuint);
else
return -1;
case GL_UNSIGNED_SHORT_8_8_MESA:
case GL_UNSIGNED_SHORT_8_8_REV_MESA:
if (format == GL_YCBCR_MESA)
return sizeof(GLushort);
else
return -1;
case GL_UNSIGNED_INT_24_8_EXT:
if (format == GL_DEPTH_STENCIL_EXT)
return sizeof(GLuint);
else
return -1;
case GL_UNSIGNED_INT_5_9_9_9_REV:
if (format == GL_RGB)
return sizeof(GLuint);
else
return -1;
case GL_UNSIGNED_INT_10F_11F_11F_REV:
if (format == GL_RGB)
return sizeof(GLuint);
else
return -1;
case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
if (format == GL_DEPTH_STENCIL)
return 8;
else
return -1;
default:
return -1;
}
}
/**
* Test if the given format is an integer (non-normalized) format.
*/
......@@ -595,3 +874,89 @@ _mesa_base_format_to_integer_format(GLenum format)
return format;
}
/**
* Does the given base texture/renderbuffer format have the channel
* named by 'pname'?
*/
GLboolean
_mesa_base_format_has_channel(GLenum base_format, GLenum pname)
{
switch (pname) {
case GL_TEXTURE_RED_SIZE:
case GL_TEXTURE_RED_TYPE:
case GL_RENDERBUFFER_RED_SIZE_EXT:
case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
if (base_format == GL_RED ||
base_format == GL_RG ||
base_format == GL_RGB ||
base_format == GL_RGBA) {
return GL_TRUE;
}
return GL_FALSE;
case GL_TEXTURE_GREEN_SIZE:
case GL_TEXTURE_GREEN_TYPE:
case GL_RENDERBUFFER_GREEN_SIZE_EXT:
case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
if (base_format == GL_RG ||
base_format == GL_RGB ||
base_format == GL_RGBA) {
return GL_TRUE;
}
return GL_FALSE;
case GL_TEXTURE_BLUE_SIZE:
case GL_TEXTURE_BLUE_TYPE:
case GL_RENDERBUFFER_BLUE_SIZE_EXT:
case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
if (base_format == GL_RGB ||
base_format == GL_RGBA) {
return GL_TRUE;
}
return GL_FALSE;
case GL_TEXTURE_ALPHA_SIZE:
case GL_TEXTURE_ALPHA_TYPE:
case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
if (base_format == GL_RGBA ||
base_format == GL_ALPHA ||
base_format == GL_LUMINANCE_ALPHA) {
return GL_TRUE;
}
return GL_FALSE;
case GL_TEXTURE_LUMINANCE_SIZE:
case GL_TEXTURE_LUMINANCE_TYPE:
if (base_format == GL_LUMINANCE ||
base_format == GL_LUMINANCE_ALPHA) {
return GL_TRUE;
}
return GL_FALSE;
case GL_TEXTURE_INTENSITY_SIZE:
case GL_TEXTURE_INTENSITY_TYPE:
if (base_format == GL_INTENSITY) {
return GL_TRUE;
}
return GL_FALSE;
case GL_TEXTURE_DEPTH_SIZE:
case GL_TEXTURE_DEPTH_TYPE:
case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
if (base_format == GL_DEPTH_STENCIL ||
base_format == GL_DEPTH_COMPONENT) {
return GL_TRUE;
}
return GL_FALSE;
case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
if (base_format == GL_DEPTH_STENCIL ||
base_format == GL_STENCIL_INDEX) {
return GL_TRUE;
}
return GL_FALSE;
default:
_mesa_warning(NULL, "%s: Unexpected channel token 0x%x\n",
__FUNCTION__, pname);
return GL_FALSE;
}
return GL_FALSE;
}
......@@ -34,6 +34,21 @@
extern "C" {
#endif
extern GLboolean
_mesa_type_is_packed(GLenum type);
extern GLint
_mesa_sizeof_type( GLenum type );
extern GLint
_mesa_sizeof_packed_type( GLenum type );
extern GLint
_mesa_components_in_format( GLenum format );
extern GLint
_mesa_bytes_per_pixel( GLenum format, GLenum type );
extern GLboolean
_mesa_is_type_integer(GLenum type);
......@@ -73,9 +88,12 @@ _mesa_is_compressed_format(struct gl_context *ctx, GLenum format);
extern GLenum
_mesa_base_format_to_integer_format(GLenum format);
extern GLboolean
_mesa_base_format_has_channel(GLenum base_format, GLenum pname);
#ifdef __cplusplus
}
#endif
#endif /* GLFORMATS_H */
......@@ -32,6 +32,7 @@
#include "glheader.h"
#include "colormac.h"
#include "glformats.h"
#include "image.h"
#include "imports.h"
#include "macros.h"
......@@ -40,40 +41,6 @@
/**
* \return GL_TRUE if type is packed pixel type, GL_FALSE otherwise.
*/
GLboolean
_mesa_type_is_packed(GLenum type)
{
switch (type) {
case GL_UNSIGNED_BYTE_3_3_2:
case GL_UNSIGNED_BYTE_2_3_3_REV:
case MESA_UNSIGNED_BYTE_4_4:
case GL_UNSIGNED_SHORT_5_6_5:
case GL_UNSIGNED_SHORT_5_6_5_REV:
case GL_UNSIGNED_SHORT_4_4_4_4:
case GL_UNSIGNED_SHORT_4_4_4_4_REV:
case GL_UNSIGNED_SHORT_5_5_5_1:
case GL_UNSIGNED_SHORT_1_5_5_5_REV:
case GL_UNSIGNED_INT_8_8_8_8:
case GL_UNSIGNED_INT_8_8_8_8_REV:
case GL_UNSIGNED_INT_10_10_10_2:
case GL_UNSIGNED_INT_2_10_10_10_REV:
case GL_UNSIGNED_SHORT_8_8_MESA:
case GL_UNSIGNED_SHORT_8_8_REV_MESA:
case GL_UNSIGNED_INT_24_8_EXT:
case GL_UNSIGNED_INT_5_9_9_9_REV:
case GL_UNSIGNED_INT_10F_11F_11F_REV:
case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
return GL_TRUE;
}
return GL_FALSE;
}
/**
* Flip the order of the 2 bytes in each word in the given array.
*
......@@ -109,252 +76,6 @@ _mesa_swap4( GLuint *p, GLuint n )
}
/**
* Get the size of a GL data type.
*
* \param type GL data type.
*
* \return the size, in bytes, of the given data type, 0 if a GL_BITMAP, or -1
* if an invalid type enum.
*/
GLint
_mesa_sizeof_type( GLenum type )
{
switch (type) {
case GL_BITMAP:
return 0;
case GL_UNSIGNED_BYTE:
return sizeof(GLubyte);
case GL_BYTE:
return sizeof(GLbyte);
case GL_UNSIGNED_SHORT:
return sizeof(GLushort);
case GL_SHORT:
return sizeof(GLshort);
case GL_UNSIGNED_INT:
return sizeof(GLuint);
case GL_INT:
return sizeof(GLint);
case GL_FLOAT:
return sizeof(GLfloat);
case GL_DOUBLE:
return sizeof(GLdouble);
case GL_HALF_FLOAT_ARB:
return sizeof(GLhalfARB);
case GL_FIXED:
return sizeof(GLfixed);
default:
return -1;
}
}
/**
* Same as _mesa_sizeof_type() but also accepting the packed pixel
* format data types.
*/
GLint
_mesa_sizeof_packed_type( GLenum type )
{
switch (type) {
case GL_BITMAP:
return 0;
case GL_UNSIGNED_BYTE:
return sizeof(GLubyte);
case GL_BYTE:
return sizeof(GLbyte);
case GL_UNSIGNED_SHORT:
return sizeof(GLushort);
case GL_SHORT:
return sizeof(GLshort);
case GL_UNSIGNED_INT:
return sizeof(GLuint);
case GL_INT:
return sizeof(GLint);
case GL_HALF_FLOAT_ARB:
return sizeof(GLhalfARB);
case GL_FLOAT:
return sizeof(GLfloat);
case GL_UNSIGNED_BYTE_3_3_2:
case GL_UNSIGNED_BYTE_2_3_3_REV:
case MESA_UNSIGNED_BYTE_4_4:
return sizeof(GLubyte);
case GL_UNSIGNED_SHORT_5_6_5:
case GL_UNSIGNED_SHORT_5_6_5_REV:
case GL_UNSIGNED_SHORT_4_4_4_4:
case GL_UNSIGNED_SHORT_4_4_4_4_REV:
case GL_UNSIGNED_SHORT_5_5_5_1:
case GL_UNSIGNED_SHORT_1_5_5_5_REV:
case GL_UNSIGNED_SHORT_8_8_MESA:
case GL_UNSIGNED_SHORT_8_8_REV_MESA:
return sizeof(GLushort);
case GL_UNSIGNED_INT_8_8_8_8:
case GL_UNSIGNED_INT_8_8_8_8_REV:
case GL_UNSIGNED_INT_10_10_10_2:
case GL_UNSIGNED_INT_2_10_10_10_REV:
case GL_UNSIGNED_INT_24_8_EXT:
case GL_UNSIGNED_INT_5_9_9_9_REV:
case GL_UNSIGNED_INT_10F_11F_11F_REV:
return sizeof(GLuint);
case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
return 8;
default:
return -1;
}
}
/**
* Get the number of components in a pixel format.
*
* \param format pixel format.
*
* \return the number of components in the given format, or -1 if a bad format.
*/
GLint
_mesa_components_in_format( GLenum format )
{
switch (format) {
case GL_COLOR_INDEX:
case GL_STENCIL_INDEX:
case GL_DEPTH_COMPONENT:
case GL_RED:
case GL_RED_INTEGER_EXT:
case GL_GREEN:
case GL_GREEN_INTEGER_EXT:
case GL_BLUE:
case GL_BLUE_INTEGER_EXT:
case GL_ALPHA:
case GL_ALPHA_INTEGER_EXT:
case GL_LUMINANCE:
case GL_LUMINANCE_INTEGER_EXT:
case GL_INTENSITY:
return 1;
case GL_LUMINANCE_ALPHA:
case GL_LUMINANCE_ALPHA_INTEGER_EXT:
case GL_RG:
case GL_YCBCR_MESA:
case GL_DEPTH_STENCIL_EXT:
case GL_DUDV_ATI:
case GL_DU8DV8_ATI:
case GL_RG_INTEGER:
return 2;
case GL_RGB:
case GL_BGR:
case GL_RGB_INTEGER_EXT:
case GL_BGR_INTEGER_EXT:
return 3;
case GL_RGBA:
case GL_BGRA:
case GL_ABGR_EXT:
case GL_RGBA_INTEGER_EXT:
case GL_BGRA_INTEGER_EXT:
return 4;