]> git.tdb.fi Git - libs/gl.git/commitdiff
Move swizzling modes to pixelformat.h
authorMikko Rasa <tdb@tdb.fi>
Fri, 12 Nov 2021 16:21:18 +0000 (18:21 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 12 Nov 2021 16:34:00 +0000 (18:34 +0200)
source/backends/opengl/pixelformat_backend.cpp
source/backends/opengl/pixelformat_backend.h
source/backends/opengl/texture_backend.cpp
source/backends/opengl/texture_backend.h
source/core/pixelformat.cpp
source/core/pixelformat.h
source/core/texture.cpp
source/core/texture.h

index 7e2a61ea536655ee8b5f33031f18ef57abe8ce59..79669ba999a75a4c9adb172153d3c10e35b0e74a 100644 (file)
 
 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
index d17bc8b0a8f0e0180b919fecae3b13f86dac22c3..0ca431e0a258d7ff927f6750826826543d28b1c3 100644 (file)
@@ -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
index 88e1c2b33877cc1349b93375b8201f4de318047c..ba6486778ac04b67504360e136f4774353b50ff3 100644 (file)
@@ -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<const Texture *>(this)->swizzle;
-       if(swizzle==Texture::NO_SWIZZLE)
+       ComponentSwizzle swizzle = static_cast<const Texture *>(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);
        }
 }
 
index a18b7bf8412d7427b17586982c3418b5408e3780..69756206ac9aa3f4966e9d13f27bede59d02ea56 100644 (file)
@@ -16,7 +16,6 @@ protected:
        unsigned target;
        std::string debug_name;
 
-       static int swizzle_orders[];
        static OpenGLTexture *scratch_binding;
 
        OpenGLTexture(unsigned);
index 81379ef07062831a85fed2ee8fa1f925f34b450a..2ee5af7de6cf7f2d073fdeaa065a6218b4ac6c51 100644 (file)
@@ -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());
index c180451403dc2af7e9f8c640a7c8384b04eb85e7..b2d1b9cd7948d069233e71cb4722043df6120f83 100644 (file)
@@ -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<PixelComponents>(f&0xFF); }
 inline unsigned get_component_count(PixelComponents c) { return c&7; }
index 5f21229fa8abc2d80c3e0029364bb519bd961bee..b8a2614a1da9b697410067a7daaad807476ace7c 100644 (file)
@@ -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);
index 639b15f5b711c820e8ff448f6d634078e2040ded..f6436c0acc8f6e51d7d8679710bbaf47ad29b67b 100644 (file)
@@ -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;