]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/pipelinestate.cpp
Use C++11 features with containers
[libs/gl.git] / source / core / pipelinestate.cpp
index a3b9e640fafee04308a9ed397c84ca3e933bbaed..30d2ab1c5ce5f5968f9c10f7e6fd0c204fa6bfe5 100644 (file)
@@ -5,14 +5,19 @@
 #include <msp/gl/extensions/arb_shader_objects.h>
 #include <msp/gl/extensions/arb_uniform_buffer_object.h>
 #include <msp/gl/extensions/arb_vertex_array_object.h>
+#include <msp/gl/extensions/msp_primitive_restart.h>
+#include "blend.h"
 #include "buffer.h"
 #include "deviceinfo.h"
+#include "depthtest.h"
+#include "framebuffer.h"
 #include "pipelinestate.h"
 #include "program.h"
+#include "rect.h"
+#include "stenciltest.h"
 #include "texture.h"
 #include "uniformblock.h"
 #include "vertexsetup.h"
-#include "windingtest.h"
 
 using namespace std;
 
@@ -21,16 +26,27 @@ 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():
+       framebuffer(0),
+       viewport(0),
+       scissor(0),
        shprog(0),
        vertex_setup(0),
-       winding_test(0),
+       front_face(COUNTERCLOCKWISE),
+       face_cull(NO_CULL),
        enabled_clip_planes(0),
+       depth_test(0),
+       stencil_test(0),
+       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()
@@ -39,40 +55,54 @@ PipelineState::~PipelineState()
                last_applied = 0;
 }
 
-void PipelineState::set_shader_program(const Program *p)
+template<typename T>
+void PipelineState::set(T &target, T value, unsigned flag)
 {
-       if(p!=shprog)
+       if(value!=target)
        {
-               shprog = p;
-               changes |= SHPROG;
+               target = value;
+               changes |= flag;
        }
 }
 
+void PipelineState::set_framebuffer(const Framebuffer *f)
+{
+       set(framebuffer, f, FRAMEBUFFER|VIEWPORT);
+}
+
+void PipelineState::set_viewport(const Rect *v)
+{
+       set(viewport, v, VIEWPORT);
+}
+
+void PipelineState::set_scissor(const Rect *s)
+{
+       set(scissor, s, SCISSOR);
+}
+
+void PipelineState::set_shader_program(const Program *p)
+{
+       set(shprog, p, SHPROG);
+}
+
 void PipelineState::set_vertex_setup(const VertexSetup *s)
 {
-       if(s!=vertex_setup)
-       {
-               vertex_setup = s;
-               changes |= VERTEX_SETUP;
-       }
+       set(vertex_setup, s, VERTEX_SETUP);
 }
 
-void PipelineState::set_winding_test(const WindingTest *w)
+void PipelineState::set_front_face(FaceWinding w)
 {
-       if(w!=winding_test)
-       {
-               winding_test = w;
-               changes |= WINDING_TEST;
-       }
+       set(front_face, w, FACE_CULL);
+}
+
+void PipelineState::set_face_cull(CullMode c)
+{
+       set(face_cull, c, FACE_CULL);
 }
 
 void PipelineState::set_enabled_clip_planes(unsigned p)
 {
-       if(p!=enabled_clip_planes)
-       {
-               enabled_clip_planes = p;
-               changes |= CLIP_PLANES;
-       }
+       set(enabled_clip_planes, p, CLIP_PLANES);
 }
 
 void PipelineState::set_texture(unsigned binding, const Texture *tex, const Sampler *samp)
@@ -80,7 +110,7 @@ void PipelineState::set_texture(unsigned binding, const Texture *tex, const Samp
        if((tex!=0)!=(samp!=0))
                throw invalid_argument("PipelineState::set_texture");
 
-       vector<BoundTexture>::iterator i = lower_bound_member(textures, binding, &BoundTexture::binding);
+       auto i = lower_bound_member(textures, binding, &BoundTexture::binding);
        if(i==textures.end() || i->binding!=binding)
                i = textures.insert(i, BoundTexture(binding));
        if(tex!=i->texture || samp!=i->sampler)
@@ -104,7 +134,7 @@ void PipelineState::set_uniform_block(unsigned binding, const BufferBackedUnifor
 
 void PipelineState::set_uniform_block_(int binding, const UniformBlock *block)
 {
-       vector<BoundUniformBlock>::iterator i = lower_bound_member(uniform_blocks, binding, &BoundUniformBlock::binding);
+       auto i = lower_bound_member(uniform_blocks, binding, &BoundUniformBlock::binding);
        if(i==uniform_blocks.end() || i->binding!=binding)
                i = uniform_blocks.insert(i, BoundUniformBlock(binding));
        if(block!=i->block || binding<0)
@@ -115,13 +145,60 @@ void PipelineState::set_uniform_block_(int binding, const UniformBlock *block)
        }
 }
 
+void PipelineState::set_depth_test(const DepthTest *dt)
+{
+       set(depth_test, dt, DEPTH_TEST);
+}
+
+void PipelineState::set_stencil_test(const StencilTest *st)
+{
+       set(stencil_test, st, STENCIL_TEST);
+}
+
+void PipelineState::set_blend(const Blend *b)
+{
+       set(blend, b, BLEND);
+}
+
 void PipelineState::apply() const
 {
+       if(!last_applied)
+               Texture::unbind_scratch();
+
        apply(this==last_applied ? changes : ~0U);
 }
 
 void PipelineState::apply(unsigned mask) const
 {
+       if(mask&FRAMEBUFFER)
+       {
+               glBindFramebuffer(GL_FRAMEBUFFER, framebuffer ? framebuffer->get_id() : 0);
+               if(framebuffer)
+               {
+                       framebuffer->refresh();
+                       framebuffer->require_complete();
+               }
+       }
+
+       if(mask&VIEWPORT)
+       {
+               if(viewport)
+                       glViewport(viewport->left, viewport->bottom, viewport->width, viewport->height);
+               else if(framebuffer)
+                       glViewport(0, 0, framebuffer->get_width(), framebuffer->get_height());
+       }
+
+       if(mask&SCISSOR)
+       {
+               if(scissor)
+               {
+                       glEnable(GL_SCISSOR_TEST);
+                       glScissor(scissor->left, scissor->bottom, scissor->width, scissor->height);
+               }
+               else
+                       glDisable(GL_SCISSOR_TEST);
+       }
+
        if(mask&SHPROG)
                glUseProgram(shprog ? shprog->get_id() : 0);
 
@@ -129,15 +206,29 @@ void PipelineState::apply(unsigned mask) const
        {
                glBindVertexArray(vertex_setup ? vertex_setup->get_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)
+                       {
+                               if(!restart_index)
+                                       glEnable(GL_PRIMITIVE_RESTART);
+                               glPrimitiveRestartIndex(ri);
+                               restart_index = ri;
+                       }
+               }
        }
 
-       if(mask&WINDING_TEST)
+       if(mask&FACE_CULL)
        {
-               if(winding_test)
+               glFrontFace(front_face==CLOCKWISE ? GL_CW : GL_CCW);
+
+               if(face_cull!=NO_CULL && front_face!=NON_MANIFOLD)
                {
                        glEnable(GL_CULL_FACE);
-                       glFrontFace(winding_test->get_winding());
+                       glCullFace(face_cull==CULL_FRONT ? GL_FRONT : GL_BACK);
                }
                else
                        glDisable(GL_CULL_FACE);
@@ -157,112 +248,90 @@ 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())
+               for(const BoundTexture &t: textures)
+                       if(t.changed || mask==~0U)
                        {
-                               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)
-                       {
-                               if(i->texture && i->sampler)
+                               if(t.texture && t.sampler)
                                {
                                        if(ARB_direct_state_access)
-                                               glBindTextureUnit(i->binding, i->texture->get_id());
+                                               glBindTextureUnit(t.binding, t.texture->get_id());
                                        else
                                        {
-                                               glActiveTexture(GL_TEXTURE0+i->binding);
-                                               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();
+                                               glActiveTexture(GL_TEXTURE0+t.binding);
+                                               if(bound_tex_targets[t.binding] && static_cast<int>(t.texture->get_target())!=bound_tex_targets[t.binding])
+                                                       glBindTexture(bound_tex_targets[t.binding], 0);
+                                               glBindTexture(t.texture->get_target(), t.texture->get_id());
                                        }
 
-                                       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;
-                                       }
+                                       bound_tex_targets[t.binding] = t.texture->get_target();
+
+                                       glBindSampler(t.binding, t.sampler->get_id());
+                                       t.sampler->refresh();
                                }
 
-                               i->changed = false;
+                               t.changed = false;
                        }
        }
 
        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)
+               for(const BoundUniformBlock &u: uniform_blocks)
+                       if(u.changed || mask==~0U)
                        {
-                               if(i->block)
+                               if(u.block)
                                {
-                                       if(i->binding>=0)
+                                       if(u.binding>=0)
                                        {
-                                               const BufferBackedUniformBlock *block = static_cast<const BufferBackedUniformBlock *>(i->block);
-                                               glBindBufferRange(GL_UNIFORM_BUFFER, i->binding, block->get_buffer()->get_id(), block->get_offset(), block->get_data_size());
+                                               const BufferBackedUniformBlock *block = static_cast<const BufferBackedUniformBlock *>(u.block);
+                                               glBindBufferRange(GL_UNIFORM_BUFFER, u.binding, block->get_buffer()->get_id(), block->get_offset(), block->get_data_size());
+                                               bound_uniform_blocks[u.binding] = 1;
                                        }
                                        else
-                                               static_cast<const DefaultUniformBlock *>(i->block)->apply();
+                                               static_cast<const DefaultUniformBlock *>(u.block)->apply();
                                }
-                               else
-                                       glBindBufferBase(GL_UNIFORM_BUFFER, i->binding, 0);
 
-                               i->changed = false;
+                               u.changed = false;
                        }
        }
 
+       if(mask&DEPTH_TEST)
+       {
+               if(depth_test && depth_test->enabled)
+               {
+                       glEnable(GL_DEPTH_TEST);
+                       glDepthFunc(get_gl_predicate(depth_test->compare));
+               }
+               else
+                       glDisable(GL_DEPTH_TEST);
+
+               glDepthMask(!depth_test || depth_test->write);
+       }
+
+       if(mask&STENCIL_TEST)
+       {
+               if(stencil_test && 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));
+               }
+               else
+                       glDisable(GL_STENCIL_TEST);
+       }
+
+       if(mask&BLEND)
+       {
+               if(blend && 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);
+               }
+               else
+                       glDisable(GL_BLEND);
+       }
+
        last_applied = this;
        changes &= ~mask;
 }
@@ -279,22 +348,30 @@ 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(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;
                        }
 
-               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);
+               glDisable(GL_DEPTH_TEST);
+               glDepthMask(true);
+               glDisable(GL_STENCIL_TEST);
+               glDisable(GL_BLEND);
 
                last_applied = 0;
        }