]> git.tdb.fi Git - libs/gl.git/commitdiff
Simplify pipeline state management
authorMikko Rasa <tdb@tdb.fi>
Mon, 13 Sep 2021 21:17:12 +0000 (00:17 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 13 Sep 2021 22:31:28 +0000 (01:31 +0300)
Instead of unbinding things immediately, defer it to when the state is
cleared.  This results in less binding calls overall.  The only reason
to unbind anything is to allow those objects to be destroyed, and that
shouldn't happen in the middle of rendering anyway.

source/core/pipelinestate.cpp
source/core/pipelinestate.h

index 0d91f759e055bb9d5a7febf80445899f2db59ff0..f855eed61e012c35d86775c32e5a8f1c6ce51a16 100644 (file)
@@ -26,6 +26,7 @@ namespace GL {
 
 const PipelineState *PipelineState::last_applied = 0;
 vector<int> PipelineState::bound_tex_targets;
+vector<char> PipelineState::bound_uniform_blocks;
 unsigned PipelineState::restart_index = 0;
 
 PipelineState::PipelineState():
@@ -42,8 +43,10 @@ PipelineState::PipelineState():
        blend(0),
        changes(0)
 {
-       if(!ARB_direct_state_access && bound_tex_targets.empty())
+       if(bound_tex_targets.empty())
                bound_tex_targets.resize(Limits::get_global().max_texture_bindings);
+       if(bound_uniform_blocks.empty())
+               bound_uniform_blocks.resize(Limits::get_global().max_uniform_bindings);
 }
 
 PipelineState::~PipelineState()
@@ -242,35 +245,6 @@ void PipelineState::apply(unsigned mask) const
 
        if(mask&TEXTURES)
        {
-               if(last_applied && this!=last_applied)
-               {
-                       vector<BoundTexture>::const_iterator i = textures.begin();
-                       vector<BoundTexture>::const_iterator j = last_applied->textures.begin();
-                       while(j!=last_applied->textures.end())
-                       {
-                               if(i==textures.end() || j->binding<i->binding)
-                               {
-                                       if(bound_tex_targets[j->binding])
-                                       {
-                                               if(ARB_direct_state_access)
-                                                       glBindTextureUnit(j->binding, 0);
-                                               else
-                                               {
-                                                       glActiveTexture(GL_TEXTURE0+j->binding);
-                                                       glBindTexture(bound_tex_targets[j->binding], 0);
-                                               }
-                                       }
-                                       ++j;
-                               }
-                               else
-                               {
-                                       if(i->binding==j->binding)
-                                               ++j;
-                                       ++i;
-                               }
-                       }
-               }
-
                for(vector<BoundTexture>::const_iterator i=textures.begin(); i!=textures.end(); ++i)
                        if(i->changed || mask==~0U)
                        {
@@ -284,23 +258,13 @@ void PipelineState::apply(unsigned mask) const
                                                if(bound_tex_targets[i->binding] && static_cast<int>(i->texture->get_target())!=bound_tex_targets[i->binding])
                                                        glBindTexture(bound_tex_targets[i->binding], 0);
                                                glBindTexture(i->texture->get_target(), i->texture->get_id());
-                                               bound_tex_targets[i->binding] = i->texture->get_target();
                                        }
 
+                                       bound_tex_targets[i->binding] = i->texture->get_target();
+
                                        glBindSampler(i->binding, i->sampler->get_id());
                                        i->sampler->refresh();
                                }
-                               else if(bound_tex_targets[i->binding])
-                               {
-                                       if(ARB_direct_state_access)
-                                               glBindTextureUnit(i->binding, 0);
-                                       else
-                                       {
-                                               glActiveTexture(GL_TEXTURE0+i->binding);
-                                               glBindTexture(bound_tex_targets[i->binding], 0);
-                                               bound_tex_targets[i->binding] = 0;
-                                       }
-                               }
 
                                i->changed = false;
                        }
@@ -308,26 +272,6 @@ void PipelineState::apply(unsigned mask) const
 
        if(mask&UNIFORMS)
        {
-               if(last_applied && this!=last_applied)
-               {
-                       vector<BoundUniformBlock>::const_iterator i = uniform_blocks.begin();
-                       vector<BoundUniformBlock>::const_iterator j = last_applied->uniform_blocks.begin();
-                       while(j!=last_applied->uniform_blocks.end())
-                       {
-                               if(i==uniform_blocks.end() || j->binding<i->binding)
-                               {
-                                       glBindBufferBase(GL_UNIFORM_BUFFER, j->binding, 0);
-                                       ++j;
-                               }
-                               else
-                               {
-                                       if(i->binding==j->binding)
-                                               ++j;
-                                       ++i;
-                               }
-                       }
-               }
-
                for(vector<BoundUniformBlock>::const_iterator i=uniform_blocks.begin(); i!=uniform_blocks.end(); ++i)
                        if(i->changed || mask==~0U)
                        {
@@ -340,9 +284,9 @@ void PipelineState::apply(unsigned mask) const
                                        }
                                        else
                                                static_cast<const DefaultUniformBlock *>(i->block)->apply();
+
+                                       bound_uniform_blocks[i->binding] = 1;
                                }
-                               else
-                                       glBindBufferBase(GL_UNIFORM_BUFFER, i->binding, 0);
 
                                i->changed = false;
                        }
@@ -402,22 +346,25 @@ void PipelineState::clear()
                        if((last_applied->enabled_clip_planes>>i)&1)
                                glDisable(GL_CLIP_PLANE0+i);
 
-               for(vector<BoundTexture>::const_iterator i=last_applied->textures.begin(); i!=last_applied->textures.end(); ++i)
-                       if(i->texture && i->sampler)
+               for(unsigned i=0; i<bound_tex_targets.size(); ++i)
+                       if(bound_tex_targets[i])
                        {
                                if(ARB_direct_state_access)
-                                       glBindTextureUnit(i->binding, 0);
+                                       glBindTextureUnit(i, 0);
                                else
                                {
-                                       glActiveTexture(GL_TEXTURE0+i->binding);
-                                       glBindTexture(bound_tex_targets[i->binding], 0);
-                                       bound_tex_targets[i->binding] = 0;
+                                       glActiveTexture(GL_TEXTURE0+i);
+                                       glBindTexture(bound_tex_targets[i], 0);
                                }
+                               bound_tex_targets[i] = 0;
                        }
 
-               for(vector<BoundUniformBlock>::const_iterator i=last_applied->uniform_blocks.begin(); i!=last_applied->uniform_blocks.end(); ++i)
-                       if(i->block)
-                               glBindBufferBase(GL_UNIFORM_BUFFER, i->binding, 0);
+               for(unsigned i=0; i<bound_uniform_blocks.size(); ++i)
+                       if(bound_uniform_blocks[i])
+                       {
+                               glBindBufferBase(GL_UNIFORM_BUFFER, i, 0);
+                               bound_uniform_blocks[i] = 0;
+                       }
 
                glDisable(GL_DEPTH_TEST);
                glDepthMask(true);
index f3c30030f67662422e995ace745ae75d8d4cbc8a..b9fac143c7b9784773970b5710de7f9bdb9e80d8 100644 (file)
@@ -76,6 +76,7 @@ private:
 
        static const PipelineState *last_applied;
        static std::vector<int> bound_tex_targets;
+       static std::vector<char> bound_uniform_blocks;
        static unsigned restart_index;
 
 public: