]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/pixelformat.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / core / pixelformat.cpp
index 1ee0cd09dcf0458fe5d010e61aca1c2d6c8a8813..6e7621882fef8eae4e595cd07e004a34b8e567bf 100644 (file)
@@ -1,11 +1,3 @@
-#include <msp/gl/extensions/arb_depth_buffer_float.h>
-#include <msp/gl/extensions/arb_depth_texture.h>
-#include <msp/gl/extensions/arb_texture_float.h>
-#include <msp/gl/extensions/arb_texture_rg.h>
-#include <msp/gl/extensions/ext_texture_srgb.h>
-#include <msp/gl/extensions/oes_required_internalformat.h>
-#include <msp/gl/extensions/oes_texture_stencil8.h>
-#include <msp/io/print.h>
 #include <msp/strings/format.h>
 #include "pixelformat.h"
 
@@ -106,108 +98,56 @@ PixelComponents components_from_graphics(Graphics::PixelFormat pf)
        }
 }
 
-PixelFormat pixelformat_from_image(const Graphics::Image &image, bool srgb)
-{
-       PixelComponents comp = components_from_graphics(image.get_format());
-       return make_pixelformat(comp, UNSIGNED_BYTE, srgb);
-}
-
-PixelFormat make_pixelformat(PixelComponents comp, DataType type, bool srgb)
+PixelComponents swizzle_components(PixelComponents comp, ComponentSwizzle swiz)
 {
-       bool normalized = !is_float(type);
-       return static_cast<PixelFormat>(comp | get_type_size(type)<<8 | (type&0x300)<<4 | normalized*0x4000 | srgb*0x8000);
+       if(swiz==NO_SWIZZLE)
+               return comp;
+       else if(comp==RED && swiz==R_TO_LUMINANCE)
+               return LUMINANCE;
+       else if(comp==RG && swiz==RG_TO_LUMINANCE_ALPHA)
+               return LUMINANCE_ALPHA;
+       else if(comp==RGB && swiz==RGB_TO_BGR)
+               return BGR;
+       else if(comp==RGBA && swiz==RGB_TO_BGR)
+               return BGRA;
+       else if(comp==RGBA && swiz==RGBA_TO_RGB)
+               return RGB;
+       else if(comp==BGRA && swiz==RGBA_TO_RGB)
+               return BGR;
+       else
+               throw invalid_argument("swizzle_components");
 }
 
-void require_pixelformat(PixelFormat pf)
+PixelComponents unswizzle_components(PixelComponents comp, ComponentSwizzle swiz)
 {
-       /* TODO These checks are only accurate for textures.  On OpenGL ES some
-       formats are allowed for render buffers earlier than textures.  In particular
-       it's possible to create a 16-bit depth renderbuffer on OpenGL ES 2.0 but
-       depth textures are only available with 3.0 or the OES_depth_texture
-       extension.*/
-       switch(pf)
-       {
-       case RGB8:
-       case RGBA8:
-               { static Require _req(OES_required_internalformat); }
-               break;
-       case R8:
-       case RG8:
-               { static Require _req(ARB_texture_rg); }
-               break;
-       case R16F:
-       case R32F:
-       case RG16F:
-       case RG32F:
-               { static Require _req(ARB_texture_rg); }
-               { static Require _req(ARB_texture_float); }
-               break;
-       case RGB16F:
-       case RGB32F:
-       case RGBA16F:
-       case RGBA32F:
-               { static Require _req(ARB_texture_float); }
-               break;
-       case SRGB8:
-       case SRGB8_ALPHA8:
-               { static Require _req(EXT_texture_sRGB); }
-               break;
-       case DEPTH_COMPONENT16:
-       case DEPTH_COMPONENT24:
-       case DEPTH_COMPONENT32:
-               { static Require _req(ARB_depth_texture); }
-               { static Require _req(OES_required_internalformat); }
-               break;
-       case DEPTH_COMPONENT32F:
-               { static Require _req(ARB_depth_buffer_float); }
-               break;
-       case STENCIL_INDEX8:
-               { static Require _req(OES_texture_stencil8); }
-               break;
-       default:
-               throw invalid_argument("require_pixelformat");
-       }
+       if(swiz==NO_SWIZZLE)
+               return comp;
+       else if(comp==LUMINANCE && swiz==R_TO_LUMINANCE)
+               return RED;
+       else if(comp==LUMINANCE_ALPHA && swiz==RG_TO_LUMINANCE_ALPHA)
+               return RG;
+       else if(comp==BGR && swiz==RGB_TO_BGR)
+               return RGB;
+       else if(comp==BGRA && swiz==RGB_TO_BGR)
+               return RGBA;
+       else if(comp==RGB && swiz==RGBA_TO_RGB)
+               return RGBA;
+       else if(comp==BGR && swiz==RGBA_TO_RGB)
+               return BGRA;
+       else
+               throw invalid_argument("swizzle_components");
 }
 
-GLenum get_gl_components(PixelComponents comp)
+PixelFormat pixelformat_from_image(const Graphics::Image &image, bool srgb)
 {
-       switch(comp)
-       {
-       case RED: return GL_RED;
-       case RG: return GL_RG;
-       case RGB: return GL_RGB;
-       case RGBA: return GL_RGBA;
-       case DEPTH_COMPONENT: return GL_DEPTH_COMPONENT;
-       case STENCIL_INDEX: return GL_STENCIL_INDEX;
-       default: throw invalid_argument("get_gl_components");
-       }
+       PixelComponents comp = components_from_graphics(image.get_format());
+       return make_pixelformat(comp, UNSIGNED_BYTE, srgb);
 }
 
-GLenum get_gl_pixelformat(PixelFormat pf)
+PixelFormat make_pixelformat(PixelComponents comp, DataType type, bool srgb)
 {
-       switch(pf)
-       {
-       case R8: return GL_R8;
-       case R16F: return GL_R16F;
-       case R32F: return GL_R32F;
-       case RG8: return GL_RG8;
-       case RG16F: return GL_RG16F;
-       case RG32F: return GL_RG32F;
-       case RGB8: return GL_RGB8;
-       case RGB16F: return GL_RGB16F;
-       case RGB32F: return GL_RGB32F;
-       case RGBA8: return GL_RGBA8;
-       case RGBA16F: return GL_RGBA16F;
-       case RGBA32F: return GL_RGBA32F;
-       case SRGB8: return GL_SRGB8;
-       case SRGB8_ALPHA8: return GL_SRGB8_ALPHA8;
-       case DEPTH_COMPONENT16: return GL_DEPTH_COMPONENT16;
-       case DEPTH_COMPONENT24: return GL_DEPTH_COMPONENT24;
-       case DEPTH_COMPONENT32: return GL_DEPTH_COMPONENT32;
-       case DEPTH_COMPONENT32F: return GL_DEPTH_COMPONENT32F;
-       case STENCIL_INDEX8: return GL_STENCIL_INDEX8;
-       default: throw invalid_argument("get_gl_pixelformat");
-       }
+       bool normalized = !is_float(type);
+       return static_cast<PixelFormat>(comp | get_type_size(type)<<8 | (type&0x300)<<4 | normalized*0x4000 | srgb*0x8000);
 }
 
 } // namespace GL