]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/pipelinestate.cpp
Convert framebuffers and related functionality to new state management
[libs/gl.git] / source / core / pipelinestate.cpp
index 673fa701e6cb6c5d5860648bab880cda117a2a87..167e7de05bfb0c629ba08327b72c5a7d3085317d 100644 (file)
@@ -5,10 +5,15 @@
 #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 "framebuffer.h"
 #include "pipelinestate.h"
 #include "program.h"
+#include "rect.h"
+#include "stenciltest.h"
 #include "texture.h"
 #include "uniformblock.h"
 #include "vertexsetup.h"
@@ -22,11 +27,17 @@ const PipelineState *PipelineState::last_applied = 0;
 vector<int> PipelineState::bound_tex_targets;
 
 PipelineState::PipelineState():
+       framebuffer(0),
+       viewport(0),
+       scissor(0),
        shprog(0),
        vertex_setup(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())
@@ -49,6 +60,21 @@ void PipelineState::set(T &target, T value, unsigned 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);
@@ -114,6 +140,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);
@@ -121,6 +162,35 @@ void PipelineState::apply() const
 
 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);
 
@@ -264,6 +334,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;
 }
@@ -297,6 +405,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;
        }
 }