From 558e1158e7b07e2a1b2fd239cd662282828990f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Sat, 5 Sep 2020 17:35:38 +0200 Subject: GLState: Add texture multibind and remove redundant binds --- source/blender/gpu/opengl/gl_state.cc | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/source/blender/gpu/opengl/gl_state.cc b/source/blender/gpu/opengl/gl_state.cc index 4831185307f..20d6d608f78 100644 --- a/source/blender/gpu/opengl/gl_state.cc +++ b/source/blender/gpu/opengl/gl_state.cc @@ -20,7 +20,10 @@ * \ingroup gpu */ +#include "BKE_global.h" + #include "BLI_math_base.h" +#include "BLI_math_bits.h" #include "GPU_extensions.h" @@ -431,6 +434,11 @@ void GLStateManager::texture_bind(Texture *tex_, eGPUSamplerState sampler_type, { BLI_assert(unit < GPU_max_textures()); GLTexture *tex = static_cast(tex_); + /* Eliminate redundant binds. */ + if ((textures_[unit] == tex->tex_id_) && + (samplers_[unit] == GLTexture::samplers_[sampler_type])) { + return; + } targets_[unit] = tex->target_; textures_[unit] = tex->tex_id_; samplers_[unit] = GLTexture::samplers_[sampler_type]; @@ -486,20 +494,25 @@ void GLStateManager::texture_bind_apply(void) if (dirty_texture_binds_ == 0) { return; } + uint64_t dirty_bind = dirty_texture_binds_; + dirty_texture_binds_ = 0; + + int first = bitscan_forward_uint64(dirty_bind); + int last = 64 - bitscan_reverse_uint64(dirty_bind); + int count = last - first; - if (false) { - /* TODO multibind */ + if (GLEW_ARB_multi_bind) { + glBindTextures(first, count, textures_ + first); + glBindSamplers(first, count, samplers_ + first); } else { - uint64_t dirty_bind = dirty_texture_binds_; - for (int unit = 0; dirty_bind != 0; dirty_bind >>= 1, unit++) { - if (dirty_bind & 1) { + for (int unit = first; unit < last; unit++) { + if ((dirty_bind >> unit) & 1UL) { glActiveTexture(GL_TEXTURE0 + unit); glBindTexture(targets_[unit], textures_[unit]); glBindSampler(unit, samplers_[unit]); } } - dirty_texture_binds_ = 0; } } -- cgit v1.2.3