From: Mikko Rasa Date: Fri, 12 Nov 2021 16:21:18 +0000 (+0200) Subject: Move swizzling modes to pixelformat.h X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=8e58fc4da8443cb67fe4cd70d6f68de2be73011d;p=libs%2Fgl.git Move swizzling modes to pixelformat.h --- diff --git a/source/backends/opengl/pixelformat_backend.cpp b/source/backends/opengl/pixelformat_backend.cpp index 7e2a61ea..79669ba9 100644 --- a/source/backends/opengl/pixelformat_backend.cpp +++ b/source/backends/opengl/pixelformat_backend.cpp @@ -11,9 +11,33 @@ using namespace std; +namespace { + +int 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_BLUE, GL_GREEN, GL_RED, GL_ALPHA +}; + +} + namespace Msp { namespace GL { +ComponentSwizzle get_required_swizzle(PixelComponents comp) +{ + switch(comp) + { + case BGR: return RGB_TO_BGR; + case BGRA: return RGB_TO_BGR; + case LUMINANCE: return R_TO_LUMINANCE; + case LUMINANCE_ALPHA: return RG_TO_LUMINANCE_ALPHA; + default: return NO_SWIZZLE; + } +} + void require_pixelformat(PixelFormat pf) { /* TODO These checks are only accurate for textures. On OpenGL ES some @@ -106,5 +130,10 @@ unsigned get_gl_pixelformat(PixelFormat pf) } } +const int *get_gl_swizzle(ComponentSwizzle swiz) +{ + return swizzle_orders+4*swiz; +} + } // namespace GL } // namespace Msp diff --git a/source/backends/opengl/pixelformat_backend.h b/source/backends/opengl/pixelformat_backend.h index d17bc8b0..0ca431e0 100644 --- a/source/backends/opengl/pixelformat_backend.h +++ b/source/backends/opengl/pixelformat_backend.h @@ -10,6 +10,7 @@ namespace GL { unsigned get_gl_components(PixelComponents); unsigned get_gl_pixelformat(PixelFormat); +const int *get_gl_swizzle(ComponentSwizzle); } // namespace GL } // namespace Msp diff --git a/source/backends/opengl/texture_backend.cpp b/source/backends/opengl/texture_backend.cpp index 88e1c2b3..ba648677 100644 --- a/source/backends/opengl/texture_backend.cpp +++ b/source/backends/opengl/texture_backend.cpp @@ -12,14 +12,6 @@ using namespace std; namespace Msp { namespace GL { -int OpenGLTexture::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_BLUE, GL_GREEN, GL_RED, GL_ALPHA -}; - OpenGLTexture *OpenGLTexture::scratch_binding = 0; OpenGLTexture::OpenGLTexture(unsigned t): @@ -63,23 +55,24 @@ void OpenGLTexture::require_swizzle() void OpenGLTexture::apply_swizzle() { - Texture::FormatSwizzle swizzle = static_cast(this)->swizzle; - if(swizzle==Texture::NO_SWIZZLE) + ComponentSwizzle swizzle = static_cast(this)->swizzle; + if(swizzle==NO_SWIZZLE) return; + const int *swizzle_order = get_gl_swizzle(swizzle); if(get_backend_api()==OPENGL_ES) { - set_parameter_i(GL_TEXTURE_SWIZZLE_R, swizzle_orders[swizzle*4]); - set_parameter_i(GL_TEXTURE_SWIZZLE_G, swizzle_orders[swizzle*4+1]); - set_parameter_i(GL_TEXTURE_SWIZZLE_B, swizzle_orders[swizzle*4+2]); - set_parameter_i(GL_TEXTURE_SWIZZLE_A, swizzle_orders[swizzle*4+3]); + set_parameter_i(GL_TEXTURE_SWIZZLE_R, swizzle_order[0]); + set_parameter_i(GL_TEXTURE_SWIZZLE_G, swizzle_order[1]); + set_parameter_i(GL_TEXTURE_SWIZZLE_B, swizzle_order[2]); + set_parameter_i(GL_TEXTURE_SWIZZLE_A, swizzle_order[3]); } else { if(ARB_direct_state_access) - glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4); + glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_order); else - glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4); + glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_order); } } diff --git a/source/backends/opengl/texture_backend.h b/source/backends/opengl/texture_backend.h index a18b7bf8..69756206 100644 --- a/source/backends/opengl/texture_backend.h +++ b/source/backends/opengl/texture_backend.h @@ -16,7 +16,6 @@ protected: unsigned target; std::string debug_name; - static int swizzle_orders[]; static OpenGLTexture *scratch_binding; OpenGLTexture(unsigned); diff --git a/source/core/pixelformat.cpp b/source/core/pixelformat.cpp index 81379ef0..2ee5af7d 100644 --- a/source/core/pixelformat.cpp +++ b/source/core/pixelformat.cpp @@ -98,6 +98,38 @@ PixelComponents components_from_graphics(Graphics::PixelFormat pf) } } +PixelComponents swizzle_components(PixelComponents comp, ComponentSwizzle swiz) +{ + 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 + throw invalid_argument("swizzle_components"); +} + +PixelComponents unswizzle_components(PixelComponents comp, ComponentSwizzle swiz) +{ + 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 + throw invalid_argument("swizzle_components"); +} + PixelFormat pixelformat_from_image(const Graphics::Image &image, bool srgb) { PixelComponents comp = components_from_graphics(image.get_format()); diff --git a/source/core/pixelformat.h b/source/core/pixelformat.h index c1804514..b2d1b9cd 100644 --- a/source/core/pixelformat.h +++ b/source/core/pixelformat.h @@ -37,6 +37,17 @@ enum PixelComponents LUMINANCE_ALPHA = 0x42 }; +/** +Describes a mapping from one set of components to another. +*/ +enum ComponentSwizzle +{ + NO_SWIZZLE, + R_TO_LUMINANCE, + RG_TO_LUMINANCE_ALPHA, + RGB_TO_BGR +}; + /** Identifies a pixel format, with components and type. @@ -89,6 +100,10 @@ void operator>>(const LexicalConverter &, PixelFormat &); PixelComponents components_from_graphics(Graphics::PixelFormat); PixelFormat pixelformat_from_image(const Graphics::Image &, bool = false); +ComponentSwizzle get_required_swizzle(PixelComponents); +PixelComponents swizzle_components(PixelComponents, ComponentSwizzle); +PixelComponents unswizzle_components(PixelComponents, ComponentSwizzle); + PixelFormat make_pixelformat(PixelComponents, DataType, bool = false); inline PixelComponents get_components(PixelFormat f) { return static_cast(f&0xFF); } inline unsigned get_component_count(PixelComponents c) { return c&7; } diff --git a/source/core/texture.cpp b/source/core/texture.cpp index 5f21229f..b8a2614a 100644 --- a/source/core/texture.cpp +++ b/source/core/texture.cpp @@ -26,28 +26,8 @@ Texture::Texture(unsigned t): void Texture::set_format(PixelFormat fmt) { PixelComponents comp = get_components(fmt); - PixelComponents st_comp = comp; - FormatSwizzle swiz = NO_SWIZZLE; - switch(comp) - { - case LUMINANCE: - st_comp = RED; - swiz = R_TO_LUMINANCE; - break; - case LUMINANCE_ALPHA: - st_comp = RG; - swiz = RG_TO_LUMINANCE_ALPHA; - break; - case BGR: - st_comp = RGB; - swiz = RGB_TO_BGR; - break; - case BGRA: - st_comp = RGBA; - swiz = RGB_TO_BGR; - break; - default:; - } + ComponentSwizzle swiz = get_required_swizzle(comp); + PixelComponents st_comp = unswizzle_components(comp, swiz); PixelFormat st_fmt = make_pixelformat(st_comp, get_component_type(fmt), is_srgb(fmt)); require_pixelformat(st_fmt); diff --git a/source/core/texture.h b/source/core/texture.h index 639b15f5..f6436c0a 100644 --- a/source/core/texture.h +++ b/source/core/texture.h @@ -70,17 +70,9 @@ public: }; protected: - enum FormatSwizzle - { - NO_SWIZZLE, - R_TO_LUMINANCE, - RG_TO_LUMINANCE_ALPHA, - RGB_TO_BGR - }; - PixelFormat format; PixelFormat storage_fmt; - FormatSwizzle swizzle; + ComponentSwizzle swizzle; bool use_srgb_format; bool auto_gen_mipmap;