]> git.tdb.fi Git - libs/gl.git/blobdiff - source/render/sequence.cpp
Redesign depth and stencil test and blend state management
[libs/gl.git] / source / render / sequence.cpp
index 6898e6688f793438cd553ea73e60ea9688df4e93..2c32db86518b14033c9452a64be2ed0fb0106397 100644 (file)
@@ -47,6 +47,9 @@ void Sequence::init(unsigned w, unsigned h)
 
 Sequence::~Sequence()
 {
+       for(vector<PostProcStep>::iterator i=postproc.begin(); i!=postproc.end(); ++i)
+               if(i->owned)
+                       delete i->postproc;
        delete target[0];
        delete target[1];
        delete target_ms;
@@ -114,25 +117,25 @@ Sequence::Step &Sequence::add_step(Tag tag, Renderable &r)
 
 void Sequence::add_postprocessor(PostProcessor &pp)
 {
-       add_postprocessor(&pp, true);
+       add_postprocessor(&pp, false);
 }
 
 void Sequence::add_postprocessor_owned(PostProcessor *pp)
 {
-       add_postprocessor(pp, false);
+       add_postprocessor(pp, true);
 }
 
-void Sequence::add_postprocessor(PostProcessor *pp, bool keep)
+void Sequence::add_postprocessor(PostProcessor *pp, bool owned)
 {
-       postproc.push_back(pp);
-       if(keep)
-               postproc.back().keep();
+       postproc.push_back(PostProcStep(pp, owned));
        try
        {
                create_targets(0);
        }
        catch(...)
        {
+               if(!owned)
+                       delete pp;
                postproc.pop_back();
                throw;
        }
@@ -140,20 +143,16 @@ void Sequence::add_postprocessor(PostProcessor *pp, bool keep)
 
 void Sequence::setup_frame(Renderer &renderer)
 {
-       for(StepList::const_iterator i=steps.begin(); i!=steps.end(); ++i)
+       for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
                if(Renderable *renderable = i->get_renderable())
                        renderable->setup_frame(renderer);
-       for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
-               i->renderable->setup_frame(renderer);
 }
 
 void Sequence::finish_frame()
 {
-       for(StepList::const_iterator i=steps.begin(); i!=steps.end(); ++i)
+       for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
                if(Renderable *renderable = i->get_renderable())
                        renderable->finish_frame();
-       for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
-               i->renderable->finish_frame();
 }
 
 void Sequence::render(Renderer &renderer, Tag tag) const
@@ -164,8 +163,6 @@ void Sequence::render(Renderer &renderer, Tag tag) const
        const Framebuffer *out_fbo = Framebuffer::current();
        // These are no-ops but will ensure the related state gets restored
        BindRestore restore_fbo(out_fbo);
-       BindRestore restore_depth_test(DepthTest::current());
-       BindRestore restore_blend(Blend::current());
 
        if(target[0])
        {
@@ -174,33 +171,27 @@ void Sequence::render(Renderer &renderer, Tag tag) const
                fbo.clear();
        }
 
-       for(StepList::const_iterator i=steps.begin(); i!=steps.end(); ++i)
+       for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
        {
-               if(const DepthTest *dt = i->get_depth_test())
-                       dt->bind();
-               else
-                       DepthTest::unbind();
+               Renderer::Push push(renderer);
 
-               if(const Blend *b = i->get_blend())
-                       b->bind();
-               else
-                       Blend::unbind();
+               renderer.set_depth_test(&i->get_depth_test());
+               renderer.set_stencil_test(&i->get_stencil_test());
+               renderer.set_blend(&i->get_blend());
 
-               renderer.set_lighting(i->get_lighting());
+               if (const Lighting *lighting = i->get_lighting())
+                       renderer.add_shader_data(lighting->get_shader_data());
                renderer.set_clipping(i->get_clipping());
 
                if(const Renderable *renderable = i->get_renderable())
                        renderer.render(*renderable, i->get_tag());
-
-               for(vector<Slot>::const_iterator j=renderables.begin(); j!=renderables.end(); ++j)
-                       if(j->passes.empty() || j->passes.count(i->get_tag()))
-                               renderer.render(*j->renderable, i->get_tag());
        }
 
        if(target[0])
        {
-               DepthTest::unbind();
-               Blend::unbind();
+               renderer.set_depth_test(0);
+               renderer.set_stencil_test(0);
+               renderer.set_blend(0);
 
                if(samples)
                        target[0]->blit_from(*target_ms);
@@ -214,7 +205,7 @@ void Sequence::render(Renderer &renderer, Tag tag) const
                                out_fbo->bind();
                        const Texture2D &color = target[j]->get_target_texture(RENDER_COLOR);
                        const Texture2D &depth = target[j]->get_target_texture(RENDER_DEPTH);
-                       postproc[i]->render(renderer, color, depth);
+                       postproc[i].postproc->render(renderer, color, depth);
                }
        }
 }
@@ -246,14 +237,39 @@ void Sequence::create_targets(unsigned recreate)
 
        if(!target_ms && samples)
                target_ms = new RenderTarget(width, height, samples, fmt);
+
+#ifdef DEBUG
+       if(!debug_name.empty())
+               set_target_debug_names();
+#endif
+}
+
+void Sequence::set_debug_name(const string &name)
+{
+#ifdef DEBUG
+       debug_name = name;
+       if(!name.empty())
+               set_target_debug_names();
+#else
+       (void)name;
+#endif
+}
+
+void Sequence::set_target_debug_names()
+{
+#ifdef DEBUG
+       for(unsigned i=0; i<2; ++i)
+               if(target[i])
+                       target[i]->set_debug_name(format("%s [RT:%d]", debug_name, i));
+       if(target_ms)
+               target_ms->set_debug_name(debug_name+" [RT:ms]");
+#endif
 }
 
 
 Sequence::Step::Step(Tag t, Renderable *r):
        tag(t),
        lighting(0),
-       depth_test(0),
-       blend(0),
        clipping(0),
        renderable(r)
 { }
@@ -263,12 +279,17 @@ void Sequence::Step::set_lighting(const Lighting *l)
        lighting = l;
 }
 
-void Sequence::Step::set_depth_test(const DepthTest *d)
+void Sequence::Step::set_depth_test(const DepthTest &dt)
 {
-       depth_test = d;
+       depth_test = dt;
 }
 
-void Sequence::Step::set_blend(const Blend *b)
+void Sequence::Step::set_stencil_test(const StencilTest &st)
+{
+       stencil_test = st;
+}
+
+void Sequence::Step::set_blend(const Blend &b)
 {
        blend = b;
 }
@@ -278,10 +299,5 @@ void Sequence::Step::set_clipping(const Clipping *c)
        clipping =c;
 }
 
-
-Sequence::Slot::Slot(Renderable *r):
-       renderable(r)
-{ }
-
 } // namespace GL
 } // namespace Msp