]> git.tdb.fi Git - libs/gl.git/blobdiff - source/backends/opengl/pipelinestate_backend.cpp
Store simpler states by value in PipelineState
[libs/gl.git] / source / backends / opengl / pipelinestate_backend.cpp
index 58e12e7ff2126040eed1b2d62580b8c0bf68cbc4..08edc5e71a05d4a04a9932fbac0ff2de4f5e8144 100644 (file)
@@ -26,41 +26,38 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
-const OpenGLPipelineState *OpenGLPipelineState::last_applied = 0;
-vector<int> OpenGLPipelineState::bound_tex_targets;
-vector<char> OpenGLPipelineState::bound_uniform_blocks;
-unsigned OpenGLPipelineState::restart_index = 0;
-unsigned OpenGLPipelineState::n_clip_distances = 0;
-
-OpenGLPipelineState::OpenGLPipelineState()
-{
-       if(bound_tex_targets.empty())
-               bound_tex_targets.resize(Device::get_current().get_info().limits.max_texture_bindings);
-       if(bound_uniform_blocks.empty())
-               bound_uniform_blocks.resize(Device::get_current().get_info().limits.max_uniform_bindings);
-}
-
 OpenGLPipelineState::~OpenGLPipelineState()
 {
-       if(this==last_applied)
-               last_applied = 0;
+       if(applied_to)
+               applied_to->get_state().last_pipeline = 0;
 }
 
 void OpenGLPipelineState::apply() const
 {
-       if(!last_applied)
-               OpenGLTexture::unbind_scratch();
+       const PipelineState &self = *static_cast<const PipelineState *>(this);
+       Device &device = Device::get_current();
+       unsigned mask = changes;
 
-       apply(this==last_applied ? static_cast<const PipelineState *>(this)->changes : ~0U);
-}
+       if(applied_to && applied_to!=&device)
+       {
+               applied_to->get_state().last_pipeline = 0;
+               mask = ~0U;
+       }
 
-void OpenGLPipelineState::apply(unsigned mask) const
-{
-       const PipelineState *self = static_cast<const PipelineState *>(this);
+       OpenGLDeviceState &dev_state = device.get_state();
+       if(!dev_state.last_pipeline)
+               OpenGLTexture::unbind_scratch();
+       
+       if(this!=dev_state.last_pipeline)
+       {
+               if(dev_state.last_pipeline)
+                       dev_state.last_pipeline->applied_to = 0;
+               mask = ~0U;
+       }
 
        if(mask&PipelineState::FRAMEBUFFER)
        {
-               const Framebuffer *framebuffer = self->framebuffer;
+               const Framebuffer *framebuffer = self.framebuffer;
                glBindFramebuffer(GL_FRAMEBUFFER, framebuffer ? framebuffer->id : 0);
                if(framebuffer)
                {
@@ -69,94 +66,88 @@ void OpenGLPipelineState::apply(unsigned mask) const
                }
        }
 
-       if(mask&(PipelineState::VIEWPORT|PipelineState::FRAMEBUFFER))
-       {
-               if(const Rect *viewport = self->viewport)
-                       glViewport(viewport->left, viewport->bottom, viewport->width, viewport->height);
-               else if(const Framebuffer *framebuffer = self->framebuffer)
-                       glViewport(0, 0, framebuffer->get_width(), framebuffer->get_height());
-       }
-
-       if(mask&PipelineState::SCISSOR)
-       {
-               if(const Rect *scissor = self->scissor)
+       if(mask&(PipelineState::VIEWPORT|PipelineState::SCISSOR|PipelineState::FRAMEBUFFER))
+               if(const Framebuffer *framebuffer = self.framebuffer)
                {
-                       glEnable(GL_SCISSOR_TEST);
-                       glScissor(scissor->left, scissor->bottom, scissor->width, scissor->height);
+                       Rect fb_rect = framebuffer->get_rect();
+                       if(mask&(PipelineState::VIEWPORT|PipelineState::FRAMEBUFFER))
+                       {
+                               Rect viewport = fb_rect.intersect(self.viewport);
+                               glViewport(viewport.left, viewport.bottom, viewport.width, viewport.height);
+                       }
+                       if(mask&(PipelineState::SCISSOR|PipelineState::FRAMEBUFFER))
+                       {
+                               Rect scissor = fb_rect.intersect(self.scissor);
+                               if(scissor!=fb_rect)
+                               {
+                                       glEnable(GL_SCISSOR_TEST);
+                                       glScissor(scissor.left, scissor.bottom, scissor.width, scissor.height);
+                               }
+                               else
+                                       glDisable(GL_SCISSOR_TEST);
+                       }
                }
-               else
-                       glDisable(GL_SCISSOR_TEST);
-       }
 
        if(mask&PipelineState::SHPROG)
        {
-               glUseProgram(self->shprog ? self->shprog->id : 0);
+               glUseProgram(self.shprog ? self.shprog->id : 0);
 
-               unsigned ncd = (self->shprog ? self->shprog->get_n_clip_distances() : 0);
-               if(ncd!=n_clip_distances)
+               unsigned ncd = (self.shprog ? self.shprog->get_n_clip_distances() : 0);
+               if(ncd!=dev_state.n_clip_distances)
                {
-                       for(unsigned i=0; (i<ncd || i<n_clip_distances); ++i)
+                       for(unsigned i=0; (i<ncd || i<dev_state.n_clip_distances); ++i)
                        {
                                if(i<ncd)
                                        glEnable(GL_CLIP_PLANE0+i);
                                else
                                        glDisable(GL_CLIP_PLANE0+i);
                        }
-                       n_clip_distances = ncd;
+                       dev_state.n_clip_distances = ncd;
                }
        }
 
-       if(mask&PipelineState::VERTEX_SETUP)
+       if(mask&PipelineState::UNIFORMS)
        {
-               const VertexSetup *vertex_setup = self->vertex_setup;
-               glBindVertexArray(vertex_setup ? vertex_setup->id : 0);
-               if(vertex_setup)
-               {
-                       static Require _req(MSP_primitive_restart);
-
-                       vertex_setup->refresh();
-                       unsigned ri = (vertex_setup->get_index_type()==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
-                       if(ri!=restart_index)
+               for(const PipelineState::BoundUniformBlock &u: self.uniform_blocks)
+                       if(u.changed || mask==~0U)
                        {
-                               if(!restart_index)
-                                       glEnable(GL_PRIMITIVE_RESTART);
-                               glPrimitiveRestartIndex(ri);
-                               restart_index = ri;
-                       }
-               }
-       }
-
-       if(mask&PipelineState::FACE_CULL)
-       {
-               glFrontFace(self->front_face==CLOCKWISE ? GL_CW : GL_CCW);
+                               if(u.used)
+                               {
+                                       if(u.binding>=0)
+                                       {
+                                               glBindBufferRange(GL_UNIFORM_BUFFER, u.binding, u.buffer->id, u.block->get_offset(), u.block->get_data_size());
+                                               dev_state.bound_uniform_blocks[u.binding] = 1;
+                                       }
+                                       else if(u.binding==ReflectData::DEFAULT_BLOCK && self.shprog)
+                                       {
+                                               const char *data = static_cast<const char *>(u.block->get_data_pointer());
+                                               for(const Program::UniformCall &call: self.shprog->uniform_calls)
+                                                       call.func(call.location, call.size, data+call.location*16);
+                                       }
+                               }
 
-               if(self->face_cull!=NO_CULL && self->front_face!=NON_MANIFOLD)
-               {
-                       glEnable(GL_CULL_FACE);
-                       glCullFace(self->face_cull==CULL_FRONT ? GL_FRONT : GL_BACK);
-               }
-               else
-                       glDisable(GL_CULL_FACE);
+                               u.changed = false;
+                       }
        }
 
        if(mask&PipelineState::TEXTURES)
        {
-               for(const PipelineState::BoundTexture &t: self->textures)
+               for(const PipelineState::BoundTexture &t: self.textures)
                        if(t.changed || mask==~0U)
                        {
-                               if(t.texture && t.sampler)
+                               if(t.used)
                                {
                                        if(ARB_direct_state_access)
                                                glBindTextureUnit(t.binding, t.texture->id);
                                        else
                                        {
                                                glActiveTexture(GL_TEXTURE0+t.binding);
-                                               if(bound_tex_targets[t.binding] && static_cast<int>(t.texture->target)!=bound_tex_targets[t.binding])
-                                                       glBindTexture(bound_tex_targets[t.binding], 0);
+                                               if(dev_state.bound_tex_targets[t.binding] && static_cast<int>(t.texture->target)!=dev_state.bound_tex_targets[t.binding])
+                                                       glBindTexture(dev_state.bound_tex_targets[t.binding], 0);
                                                glBindTexture(t.texture->target, t.texture->id);
                                        }
 
-                                       bound_tex_targets[t.binding] = t.texture->target;
+                                       dev_state.bound_tex_targets[t.binding] = t.texture->target;
 
                                        glBindSampler(t.binding, t.sampler->id);
                                        t.sampler->refresh();
@@ -166,52 +157,61 @@ void OpenGLPipelineState::apply(unsigned mask) const
                        }
        }
 
-       if(mask&PipelineState::UNIFORMS)
+       if(mask&PipelineState::VERTEX_SETUP)
        {
-               for(const PipelineState::BoundUniformBlock &u: self->uniform_blocks)
-                       if(u.changed || mask==~0U)
-                       {
-                               if(u.block)
-                               {
-                                       if(u.binding>=0)
-                                       {
-                                               glBindBufferRange(GL_UNIFORM_BUFFER, u.binding, u.block->get_buffer()->id, u.block->get_offset(), u.block->get_data_size());
-                                               bound_uniform_blocks[u.binding] = 1;
-                                       }
-                                       else if(self->shprog)
-                                       {
-                                               const char *data = static_cast<const char *>(u.block->get_data_pointer());
-                                               for(const Program::UniformCall &call: self->shprog->uniform_calls)
-                                                       call.func(call.location, call.size, data+call.location*16);
-                                       }
-                               }
+               const VertexSetup *vertex_setup = self.vertex_setup;
+               glBindVertexArray(vertex_setup ? vertex_setup->id : 0);
+               if(vertex_setup)
+               {
+                       static Require _req(MSP_primitive_restart);
 
-                               u.changed = false;
+                       vertex_setup->refresh();
+                       unsigned ri = (vertex_setup->get_index_type()==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
+                       if(ri!=dev_state.restart_index)
+                       {
+                               if(!dev_state.restart_index)
+                                       glEnable(GL_PRIMITIVE_RESTART);
+                               glPrimitiveRestartIndex(ri);
+                               dev_state.restart_index = ri;
                        }
+               }
+       }
+
+       if(mask&PipelineState::FACE_CULL)
+       {
+               glFrontFace(self.front_face==CLOCKWISE ? GL_CW : GL_CCW);
+
+               if(self.face_cull!=NO_CULL && self.front_face!=NON_MANIFOLD)
+               {
+                       glEnable(GL_CULL_FACE);
+                       glCullFace(self.face_cull==CULL_FRONT ? GL_FRONT : GL_BACK);
+               }
+               else
+                       glDisable(GL_CULL_FACE);
        }
 
        if(mask&PipelineState::DEPTH_TEST)
        {
-               const DepthTest *depth_test = self->depth_test;
-               if(depth_test && depth_test->enabled)
+               const DepthTest &depth_test = self.depth_test;
+               if(depth_test.enabled)
                {
                        glEnable(GL_DEPTH_TEST);
-                       glDepthFunc(get_gl_predicate(depth_test->compare));
+                       glDepthFunc(get_gl_predicate(depth_test.compare));
                }
                else
                        glDisable(GL_DEPTH_TEST);
 
-               glDepthMask(!depth_test || depth_test->write);
+               glDepthMask(depth_test.write);
        }
 
        if(mask&PipelineState::STENCIL_TEST)
        {
-               const StencilTest *stencil_test = self->stencil_test;
-               if(stencil_test && stencil_test->enabled)
+               const StencilTest &stencil_test = self.stencil_test;
+               if(stencil_test.enabled)
                {
                        glEnable(GL_STENCIL_TEST);
-                       glStencilFunc(get_gl_predicate(stencil_test->compare), stencil_test->reference, 0xFFFFFFFF);
-                       glStencilOp(get_gl_stencil_op(stencil_test->stencil_fail_op), get_gl_stencil_op(stencil_test->depth_fail_op), get_gl_stencil_op(stencil_test->depth_pass_op));
+                       glStencilFunc(get_gl_predicate(stencil_test.compare), stencil_test.reference, 0xFFFFFFFF);
+                       glStencilOp(get_gl_stencil_op(stencil_test.stencil_fail_op), get_gl_stencil_op(stencil_test.depth_fail_op), get_gl_stencil_op(stencil_test.depth_pass_op));
                }
                else
                        glDisable(GL_STENCIL_TEST);
@@ -219,14 +219,14 @@ void OpenGLPipelineState::apply(unsigned mask) const
 
        if(mask&PipelineState::BLEND)
        {
-               const Blend *blend = self->blend;
-               if(blend && blend->enabled)
+               const Blend &blend = self.blend;
+               if(blend.enabled)
                {
                        glEnable(GL_BLEND);
-                       glBlendEquation(get_gl_blend_equation(blend->equation));
-                       glBlendFunc(get_gl_blend_factor(blend->src_factor), get_gl_blend_factor(blend->dst_factor));
-                       glBlendColor(blend->constant.r, blend->constant.g, blend->constant.b, blend->constant.a);
-                       ColorWriteMask cw = blend->write_mask;
+                       glBlendEquation(get_gl_blend_equation(blend.equation));
+                       glBlendFunc(get_gl_blend_factor(blend.src_factor), get_gl_blend_factor(blend.dst_factor));
+                       glBlendColor(blend.constant.r, blend.constant.g, blend.constant.b, blend.constant.a);
+                       ColorWriteMask cw = blend.write_mask;
                        glColorMask((cw&WRITE_RED)!=0, (cw&WRITE_GREEN)!=0, (cw&WRITE_BLUE)!=0, (cw&WRITE_ALPHA)!=0);
                }
                else
@@ -236,39 +236,41 @@ void OpenGLPipelineState::apply(unsigned mask) const
                }
        }
 
-       last_applied = this;
-       self->changes &= ~mask;
+       applied_to = &device;
+       dev_state.last_pipeline = this;
+       changes = 0;
 }
 
 void OpenGLPipelineState::clear()
 {
-       if(last_applied)
+       OpenGLDeviceState &dev_state = Device::get_current().get_state();
+       if(dev_state.last_pipeline)
        {
                glUseProgram(0);
                glBindVertexArray(0);
 
-               for(unsigned i=0; i<n_clip_distances; ++i)
+               for(unsigned i=0; i<dev_state.n_clip_distances; ++i)
                        glDisable(GL_CLIP_PLANE0+i);
-               n_clip_distances = 0;
+               dev_state.n_clip_distances = 0;
 
-               for(unsigned i=0; i<bound_tex_targets.size(); ++i)
-                       if(bound_tex_targets[i])
+               for(unsigned i=0; i<dev_state.bound_tex_targets.size(); ++i)
+                       if(dev_state.bound_tex_targets[i])
                        {
                                if(ARB_direct_state_access)
                                        glBindTextureUnit(i, 0);
                                else
                                {
                                        glActiveTexture(GL_TEXTURE0+i);
-                                       glBindTexture(bound_tex_targets[i], 0);
+                                       glBindTexture(dev_state.bound_tex_targets[i], 0);
                                }
-                               bound_tex_targets[i] = 0;
+                               dev_state.bound_tex_targets[i] = 0;
                        }
 
-               for(unsigned i=0; i<bound_uniform_blocks.size(); ++i)
-                       if(bound_uniform_blocks[i])
+               for(unsigned i=0; i<dev_state.bound_uniform_blocks.size(); ++i)
+                       if(dev_state.bound_uniform_blocks[i])
                        {
                                glBindBufferBase(GL_UNIFORM_BUFFER, i, 0);
-                               bound_uniform_blocks[i] = 0;
+                               dev_state.bound_uniform_blocks[i] = 0;
                        }
 
                glDisable(GL_DEPTH_TEST);
@@ -276,7 +278,8 @@ void OpenGLPipelineState::clear()
                glDisable(GL_STENCIL_TEST);
                glDisable(GL_BLEND);
 
-               last_applied = 0;
+               dev_state.last_pipeline->applied_to = 0;
+               dev_state.last_pipeline = 0;
        }
 }