]> git.tdb.fi Git - libs/gl.git/commitdiff
Simplify applying texture swizzling
authorMikko Rasa <tdb@tdb.fi>
Sun, 31 Jan 2021 11:11:52 +0000 (13:11 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 31 Jan 2021 11:57:07 +0000 (13:57 +0200)
It's now set once when creating storage for the texture, so tracking its
dirty state is not necessary.

source/texture.cpp
source/texture.h
source/texture1d.cpp
source/texture2d.cpp
source/texture3d.cpp
source/texturecube.cpp

index 7983cfb50e5c4272f76c2ceb9e1c3a5628639635..90e3bb17f11de1c318f6670a28ce61a17a13a88b 100644 (file)
@@ -25,8 +25,8 @@ Texture::Texture(GLenum t, ResourceManager *m):
        id(0),
        target(t),
        ifmt(RGB),
+       swizzle(NO_SWIZZLE),
        auto_gen_mipmap(false),
-       dirty_params(0),
        default_sampler(*this)
 {
        if(m)
@@ -53,18 +53,18 @@ DataType Texture::get_alloc_type(PixelFormat fmt)
 
 void Texture::set_internal_format(PixelFormat fmt)
 {
-       FormatSwizzle swiz = NO_SWIZZLE;
+       swizzle = NO_SWIZZLE;
        if(ARB_texture_rg && ARB_texture_swizzle)
        {
                if(fmt==LUMINANCE)
                {
                        fmt = RED;
-                       swiz = R_TO_LUMINANCE;
+                       swizzle = R_TO_LUMINANCE;
                }
                else if(fmt==LUMINANCE_ALPHA)
                {
                        fmt = RG;
-                       swiz = RG_TO_LUMINANCE_ALPHA;
+                       swizzle = RG_TO_LUMINANCE_ALPHA;
                }
        }
 
@@ -73,9 +73,6 @@ void Texture::set_internal_format(PixelFormat fmt)
 
        require_pixelformat(fmt);
        ifmt = fmt;
-       swizzle = swiz;
-       if(swizzle)
-               update_parameter(FORMAT_SWIZZLE);
 }
 
 PixelFormat Texture::get_upload_format(PixelFormat fmt) const
@@ -86,42 +83,24 @@ PixelFormat Texture::get_upload_format(PixelFormat fmt) const
                return fmt;
 }
 
-void Texture::update_parameter(int mask) const
+void Texture::apply_swizzle()
 {
-       if(!id)
-       {
-               dirty_params |= mask;
+       if(swizzle==NO_SWIZZLE)
                return;
-       }
 
-       if(!ARB_direct_state_access && TexUnit::current().get_texture()!=this)
+       if(get_gl_api()==OPENGL_ES2)
        {
-               TexUnit *unit = TexUnit::find_unit(this);
-               if(!unit)
-               {
-                       dirty_params |= mask;
-                       return;
-               }
-
-               unit->bind();
+               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]);
        }
-
-       if(mask&FORMAT_SWIZZLE)
+       else
        {
-               if(get_gl_api()==OPENGL_ES2)
-               {
-                       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]);
-               }
+               if(ARB_direct_state_access)
+                       glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
                else
-               {
-                       if(ARB_direct_state_access)
-                               glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
-                       else
-                               glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
-               }
+                       glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
        }
 }
 
@@ -133,14 +112,6 @@ void Texture::set_parameter_i(GLenum param, int value) const
                glTexParameteri(target, param, value);
 }
 
-void Texture::set_parameter_f(GLenum param, float value) const
-{
-       if(ARB_direct_state_access)
-               glTextureParameterf(id, param, value);
-       else
-               glTexParameterf(target, param, value);
-}
-
 void Texture::set_min_filter(TextureFilter f)
 {
        default_sampler.set_min_filter(f);
@@ -266,12 +237,6 @@ void Texture::bind_to(unsigned i) const
                        glBindTexture(target, id);
                }
 
-               if(dirty_params)
-               {
-                       update_parameter(dirty_params);
-                       dirty_params = 0;
-               }
-
                default_sampler.bind_to(i);
        }
 }
index ad41e573ed5d53b409f82794ae44afec96a1a23e..adcfa76b7245276078af013622f343dca8ac65f0 100644 (file)
@@ -72,7 +72,6 @@ protected:
        PixelFormat ifmt;
        FormatSwizzle swizzle;
        bool auto_gen_mipmap;
-       mutable int dirty_params;
        Sampler default_sampler;
 
        static int swizzle_orders[];
@@ -87,16 +86,13 @@ protected:
        static DataType get_alloc_type(PixelFormat);
        void set_internal_format(PixelFormat);
        PixelFormat get_upload_format(PixelFormat) const;
+       void apply_swizzle();
+       void set_parameter_i(GLenum, int) const;
 
 public:
        Sampler &get_default_sampler() { return default_sampler; }
        const Sampler &get_default_sampler() const { return default_sampler; }
 
-protected:
-       void update_parameter(int) const;
-       void set_parameter_i(GLenum, int) const;
-       void set_parameter_f(GLenum, float) const;
-public:
        DEPRECATED void set_min_filter(TextureFilter);
        DEPRECATED void set_mag_filter(TextureFilter);
 
index 7163aa7a78d846ed834c32abcdb67fcc99201aa8..b50f8cf444c0c9fb1435ce97e756ffc1330b59c2 100644 (file)
@@ -44,13 +44,12 @@ void Texture1D::allocate(unsigned level)
 
        if(ARB_texture_storage)
        {
+               Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
                if(ARB_direct_state_access)
                        glTextureStorage1D(id, levels, ifmt, width);
                else
-               {
-                       BindRestore _bind(this);
                        glTexStorage1D(target, levels, ifmt, width);
-               }
+               apply_swizzle();
                allocated |= (1<<levels)-1;
        }
        else
@@ -73,7 +72,10 @@ void Texture1D::image(unsigned level, PixelFormat fmt, DataType type, const void
        BindRestore _bind(this);
 
        if(!allocated)
+       {
                glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
+               apply_swizzle();
+       }
        glTexImage1D(target, level, ifmt, w, 0, get_upload_format(fmt), type, data);
 
        allocated |= 1<<level;
index 78011d3046ea5fb8189f823096fa8d730c651033..559167f8ba4d5eefc017e6e158d76dec55127cf6 100644 (file)
@@ -72,13 +72,12 @@ void Texture2D::allocate(unsigned level)
 
        if(ARB_texture_storage)
        {
+               Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
                if(ARB_direct_state_access)
                        glTextureStorage2D(id, levels, ifmt, width, height);
                else
-               {
-                       BindRestore _bind(this);
                        glTexStorage2D(target, levels, ifmt, width, height);
-               }
+               apply_swizzle();
                allocated |= (1<<levels)-1;
        }
        else
@@ -103,7 +102,10 @@ void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void
        BindRestore _bind(this);
 
        if(!allocated)
+       {
                glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
+               apply_swizzle();
+       }
        glTexImage2D(target, level, ifmt, w, h, 0, get_upload_format(fmt), type, data);
 
        allocated |= 1<<level;
@@ -189,8 +191,6 @@ void Texture2D::unload()
        glDeleteTextures(1, &id);
        id = 0;
        allocated = 0;
-       // TODO check which params actually need refreshing
-       dirty_params = -1;
        default_sampler.unload();
 }
 
index c8f59a094a0fed550a37a5721ae26329941a54ae..d22070e680e9d67272a0a7e47d71ffd8aa1a7233 100644 (file)
@@ -60,13 +60,12 @@ void Texture3D::allocate(unsigned level)
 
        if(ARB_texture_storage)
        {
+               Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
                if(ARB_direct_state_access)
                        glTextureStorage3D(id, levels, ifmt, width, height, depth);
                else
-               {
-                       BindRestore _bind(this);
                        glTexStorage3D(target, levels, ifmt, width, height, depth);
-               }
+               apply_swizzle();
                allocated |= (1<<levels)-1;
        }
        else
@@ -92,7 +91,10 @@ void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void
        BindRestore _bind(this);
 
        if(!allocated)
+       {
                glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
+               apply_swizzle();
+       }
        glTexImage3D(target, level, ifmt, width, height, depth, 0, get_upload_format(fmt), type, data);
 
        allocated |= 1<<level;
index 0613a39a8ebba335a56f42a9ec958c9a796ac45e..8d194c42028bb6a4c6eb56cf2e9c0cb9567eba71 100644 (file)
@@ -78,6 +78,7 @@ void TextureCube::allocate(unsigned level)
        {
                BindRestore _bind(this);
                glTexStorage2D(target, levels, ifmt, size, size);
+               apply_swizzle();
                allocated |= (1<<levels)-1;
        }
        else
@@ -104,7 +105,10 @@ void TextureCube::image(TextureCubeFace face, unsigned level, PixelFormat fmt, D
        BindRestore _bind(this);
 
        if(!allocated)
+       {
                glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
+               apply_swizzle();
+       }
        glTexImage2D(face, level, ifmt, s, s, 0, get_upload_format(fmt), type, data);
 
        if(level==0)