From: Mikko Rasa Date: Sun, 31 Jan 2021 11:11:52 +0000 (+0200) Subject: Simplify applying texture swizzling X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=73eec11d44a24bac121f1b0d85f20d58005f3545;p=libs%2Fgl.git Simplify applying texture swizzling It's now set once when creating storage for the texture, so tracking its dirty state is not necessary. --- 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<