From 73eec11d44a24bac121f1b0d85f20d58005f3545 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 31 Jan 2021 13:11:52 +0200 Subject: [PATCH] Simplify applying texture swizzling It's now set once when creating storage for the texture, so tracking its dirty state is not necessary. --- source/texture.cpp | 65 ++++++++++-------------------------------- source/texture.h | 8 ++---- source/texture1d.cpp | 8 ++++-- source/texture2d.cpp | 10 +++---- source/texture3d.cpp | 8 ++++-- source/texturecube.cpp | 4 +++ 6 files changed, 36 insertions(+), 67 deletions(-) diff --git a/source/texture.cpp b/source/texture.cpp index 7983cfb5..90e3bb17 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -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); } } diff --git a/source/texture.h b/source/texture.h index ad41e573..adcfa76b 100644 --- a/source/texture.h +++ b/source/texture.h @@ -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); diff --git a/source/texture1d.cpp b/source/texture1d.cpp index 7163aa7a..b50f8cf4 100644 --- a/source/texture1d.cpp +++ b/source/texture1d.cpp @@ -44,13 +44,12 @@ void Texture1D::allocate(unsigned level) if(ARB_texture_storage) { + Conditional _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< _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< _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<