]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/pipelinestate.cpp
Redesign depth and stencil test and blend state management
[libs/gl.git] / source / core / pipelinestate.cpp
index a3b9e640fafee04308a9ed397c84ca3e933bbaed..7a38eab5e4136a12e490e4bdcbb079872e181de1 100644 (file)
@@ -5,14 +5,16 @@
 #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 "blend.h"
 #include "buffer.h"
 #include "deviceinfo.h"
+#include "depthtest.h"
 #include "pipelinestate.h"
 #include "program.h"
+#include "stenciltest.h"
 #include "texture.h"
 #include "uniformblock.h"
 #include "vertexsetup.h"
-#include "windingtest.h"
 
 using namespace std;
 
@@ -25,8 +27,12 @@ vector<int> PipelineState::bound_tex_targets;
 PipelineState::PipelineState():
        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())
@@ -39,40 +45,39 @@ 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_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)
@@ -115,6 +120,21 @@ 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
 {
        apply(this==last_applied ? changes : ~0U);
@@ -132,12 +152,14 @@ void PipelineState::apply(unsigned mask) const
                        vertex_setup->refresh();
        }
 
-       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);
@@ -187,7 +209,7 @@ void PipelineState::apply(unsigned mask) const
                }
 
                for(vector<BoundTexture>::const_iterator i=textures.begin(); i!=textures.end(); ++i)
-                       if(i->changed)
+                       if(i->changed || mask==~0U)
                        {
                                if(i->texture && i->sampler)
                                {
@@ -244,7 +266,7 @@ void PipelineState::apply(unsigned mask) const
                }
 
                for(vector<BoundUniformBlock>::const_iterator i=uniform_blocks.begin(); i!=uniform_blocks.end(); ++i)
-                       if(i->changed)
+                       if(i->changed || mask==~0U)
                        {
                                if(i->block)
                                {
@@ -263,6 +285,44 @@ void PipelineState::apply(unsigned mask) const
                        }
        }
 
+       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;
 }
@@ -296,6 +356,11 @@ void PipelineState::clear()
                        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;
        }
 }