]> git.tdb.fi Git - libs/gl.git/commitdiff
Support BGR and BGRA as texture formats through swizzling
authorMikko Rasa <tdb@tdb.fi>
Mon, 1 Feb 2021 19:18:29 +0000 (21:18 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 1 Feb 2021 20:25:37 +0000 (22:25 +0200)
source/pixelformat.cpp
source/pixelformat.h
source/texture.cpp
source/texture.h

index cc6a6d007067a671dd8e849ba8e9ad4951787e74..94bf9716660d8c56054ce55fa7809e519695ca3f 100644 (file)
@@ -64,6 +64,10 @@ void operator>>(const LexicalConverter &conv, PixelFormat &fmt)
                fmt = SRGB8;
        else if(conv.get()=="SRGB8_ALPHA8")
                fmt = SRGB8_ALPHA8;
+       else if(conv.get()=="BGR8")
+               fmt = BGR8;
+       else if(conv.get()=="BGRA8")
+               fmt = BGRA8;
        else if(conv.get()=="LUMINANCE8")
                fmt = LUMINANCE8;
        else if(conv.get()=="LUMINANCE8_ALPHA8")
@@ -127,7 +131,7 @@ PixelFormat pixelformat_from_image(const Graphics::Image &image)
 
 PixelFormat make_pixelformat(PixelComponents comp, DataType type, bool srgb)
 {
-       if(srgb && type!=UNSIGNED_BYTE && comp!=RGB && comp!=RGBA)
+       if(srgb && type!=UNSIGNED_BYTE && comp!=RGB && comp!=RGBA && comp!=BGR && comp!=BGRA)
                throw invalid_argument("make_pixelformat");
 
        switch(comp)
@@ -164,6 +168,14 @@ PixelFormat make_pixelformat(PixelComponents comp, DataType type, bool srgb)
                case FLOAT: return RGBA32F;
                default: throw invalid_argument("make_pixelformat");
                }
+       case BGR:
+               if(type!=UNSIGNED_BYTE)
+                       throw invalid_argument("make_pixelformat");
+               return (srgb ? BGR8 : SBGR8);
+       case BGRA:
+               if(type!=UNSIGNED_BYTE)
+                       throw invalid_argument("make_pixelformat");
+               return (srgb ? BGRA8 : SBGR8_ALPHA8);
        case LUMINANCE:
                if(type!=UNSIGNED_BYTE)
                        throw invalid_argument("make_pixelformat");
@@ -217,6 +229,10 @@ PixelComponents get_components(PixelFormat pf)
        case RGBA16F:
        case RGBA32F:
        case SRGB8_ALPHA8: return RGBA;
+       case BGR8:
+       case SBGR8: return BGR;
+       case BGRA8:
+       case SBGR8_ALPHA8: return BGRA;
        case LUMINANCE8: return LUMINANCE;
        case LUMINANCE8_ALPHA8: return LUMINANCE_ALPHA;
        case STENCIL_INDEX8: return STENCIL_INDEX;
@@ -284,6 +300,10 @@ DataType get_component_type(PixelFormat pf)
        case RGBA8:
        case SRGB8:
        case SRGB8_ALPHA8:
+       case BGR8:
+       case BGRA8:
+       case SBGR8:
+       case SBGR8_ALPHA8:
        case LUMINANCE8:
        case LUMINANCE8_ALPHA8:
                return UNSIGNED_BYTE;
index 9dec8234988e30e68d34f5d0a41a33cd225a21a6..96f859cae47ad0b46ad1b5f6b06d4b137057f30e 100644 (file)
@@ -49,6 +49,10 @@ enum PixelFormat
        RGBA32F         = GL_RGBA32F,
        SRGB8           = GL_SRGB8,
        SRGB8_ALPHA8    = GL_SRGB8_ALPHA8,
+       BGR8            = 200000,
+       BGRA8           = 200001,
+       SBGR8           = 200002,
+       SBGR8_ALPHA8    = 200003,
        LUMINANCE8      = GL_LUMINANCE8,
        LUMINANCE8_ALPHA8 = GL_LUMINANCE8_ALPHA8,
        DEPTH_COMPONENT16 = GL_DEPTH_COMPONENT16,
index f0c55999250263f641e546341e22b7545177748b..cf32ca301cab4bf10bc513ad65de9546c1b757e4 100644 (file)
@@ -18,7 +18,8 @@ int Texture::swizzle_orders[] =
 {
        GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA,
        GL_RED, GL_RED, GL_RED, GL_ONE,
-       GL_RED, GL_RED, GL_RED, GL_GREEN
+       GL_RED, GL_RED, GL_RED, GL_GREEN,
+       GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA
 };
 
 Texture::Texture(GLenum t, ResourceManager *m):
@@ -60,6 +61,14 @@ void Texture::set_internal_format(PixelFormat fmt)
                comp = RG;
                swiz = RG_TO_LUMINANCE_ALPHA;
                break;
+       case BGR:
+               comp = RGB;
+               swiz = RGB_TO_BGR;
+               break;
+       case BGRA:
+               comp = RGBA;
+               swiz = RGB_TO_BGR;
+               break;
        default:;
        }
 
@@ -73,7 +82,7 @@ void Texture::set_internal_format(PixelFormat fmt)
 
 PixelComponents Texture::get_upload_components(PixelComponents comp) const
 {
-       if(comp==LUMINANCE || comp==LUMINANCE_ALPHA)
+       if(comp==LUMINANCE || comp==LUMINANCE_ALPHA || comp==BGR || comp==BGRA)
                return get_components(ifmt);
        else
                return comp;
index 71192fef06c42416b8a9dadc26f1fe2109626a67..6c0fbf2574b6cb6f8a897e62f0977b8db9486e4f 100644 (file)
@@ -64,7 +64,8 @@ protected:
        {
                NO_SWIZZLE,
                R_TO_LUMINANCE,
-               RG_TO_LUMINANCE_ALPHA
+               RG_TO_LUMINANCE_ALPHA,
+               RGB_TO_BGR
        };
 
        unsigned id;