From 2edfcf08707c3627b6b27289ba607a7183d63b01 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 1 Feb 2021 21:18:29 +0200 Subject: [PATCH] Support BGR and BGRA as texture formats through swizzling --- source/pixelformat.cpp | 22 +++++++++++++++++++++- source/pixelformat.h | 4 ++++ source/texture.cpp | 13 +++++++++++-- source/texture.h | 3 ++- 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/source/pixelformat.cpp b/source/pixelformat.cpp index cc6a6d00..94bf9716 100644 --- a/source/pixelformat.cpp +++ b/source/pixelformat.cpp @@ -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; diff --git a/source/pixelformat.h b/source/pixelformat.h index 9dec8234..96f859ca 100644 --- a/source/pixelformat.h +++ b/source/pixelformat.h @@ -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, diff --git a/source/texture.cpp b/source/texture.cpp index f0c55999..cf32ca30 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -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; diff --git a/source/texture.h b/source/texture.h index 71192fef..6c0fbf25 100644 --- a/source/texture.h +++ b/source/texture.h @@ -64,7 +64,8 @@ protected: { NO_SWIZZLE, R_TO_LUMINANCE, - RG_TO_LUMINANCE_ALPHA + RG_TO_LUMINANCE_ALPHA, + RGB_TO_BGR }; unsigned id; -- 2.43.0