]> git.tdb.fi Git - libs/gl.git/blobdiff - source/pipeline.cpp
Remove dynamic allocation from VertexFormat
[libs/gl.git] / source / pipeline.cpp
index d7e175de7185fe39fcf890c2c8eeb89c8986346a..dc1eba1d117cdcca5d6b190c53b3545ed31fd86c 100644 (file)
@@ -40,8 +40,17 @@ void Pipeline::set_hdr(bool h)
        if(h==hdr)
                return;
 
+       bool old_hdr= hdr;
        hdr = h;
-       create_targets(2);
+       try
+       {
+               create_targets(2);
+       }
+       catch(...)
+       {
+               hdr = old_hdr;
+               throw;
+       }
 }
 
 void Pipeline::set_multisample(unsigned s)
@@ -69,7 +78,7 @@ void Pipeline::set_camera(const Camera *c)
 
 Pipeline::Pass &Pipeline::add_pass(const Tag &tag)
 {
-       passes.push_back(Pass(tag));
+       passes.push_back(Pass(tag, 0));
        return passes.back();
 }
 
@@ -108,15 +117,32 @@ void Pipeline::remove_renderable(const Renderable &r)
                }
 }
 
+Pipeline::Pass &Pipeline::add_pass(const Tag &tag, const Renderable &r)
+{
+       passes.push_back(Pass(tag, &r));
+       return passes.back();
+}
+
 void Pipeline::add_postprocessor(PostProcessor &pp)
 {
        postproc.push_back(&pp);
-       create_targets(0);
+       try
+       {
+               create_targets(0);
+       }
+       catch(...)
+       {
+               postproc.pop_back();
+               throw;
+       }
 }
 
 void Pipeline::setup_frame() const
 {
        in_frame = true;
+       for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
+               if(const Renderable *renderable = i->get_renderable())
+                       renderable->setup_frame();
        for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
                i->renderable->setup_frame();
 }
@@ -124,6 +150,9 @@ void Pipeline::setup_frame() const
 void Pipeline::finish_frame() const
 {
        in_frame = false;
+       for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
+               if(const Renderable *renderable = i->get_renderable())
+                       renderable->finish_frame();
        for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
                i->renderable->finish_frame();
 }
@@ -147,9 +176,10 @@ void Pipeline::render(Renderer &renderer, const Tag &tag) const
                setup_frame();
 
        const Framebuffer *out_fbo = Framebuffer::current();
-       /* Binding the current object is a no-op, but this will restore the original
-       FBO in case an exception is thrown. */
-       Bind restore_fbo(out_fbo, true);
+       // These is a 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])
        {
@@ -158,11 +188,24 @@ void Pipeline::render(Renderer &renderer, const Tag &tag) const
                fbo.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
        }
 
+
        for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
        {
-               Bind bind_depth_test(i->get_depth_test());
-               Bind bind_blend(i->get_blend());
+               if(const DepthTest *dt = i->get_depth_test())
+                       dt->bind();
+               else
+                       DepthTest::unbind();
+
+               if(const Blend *b = i->get_blend())
+                       b->bind();
+               else
+                       Blend::unbind();
+
                renderer.set_lighting(i->get_lighting());
+               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()))
@@ -171,6 +214,9 @@ void Pipeline::render(Renderer &renderer, const Tag &tag) const
 
        if(target[0])
        {
+               BindRestore unbind_depth_test(static_cast<DepthTest *>(0));
+               BindRestore unbind_blend(static_cast<Blend *>(0));
+
                if(samples)
                        target[0]->fbo.blit_from(target_ms->fbo, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false);
 
@@ -181,10 +227,8 @@ void Pipeline::render(Renderer &renderer, const Tag &tag) const
                                target[1-j]->fbo.bind();
                        else
                                out_fbo->bind();
-                       postproc[i]->render(target[j]->color, target[j]->depth);
+                       postproc[i]->render(renderer, target[j]->color, target[j]->depth);
                }
-
-               out_fbo->bind();
        }
 
        if(!was_in_frame)
@@ -220,11 +264,13 @@ void Pipeline::create_targets(unsigned recreate)
 }
 
 
-Pipeline::Pass::Pass(const Tag &t):
+Pipeline::Pass::Pass(const Tag &t, const Renderable *r):
        tag(t),
        lighting(0),
        depth_test(0),
-       blend(0)
+       blend(0),
+       clipping(0),
+       renderable(r)
 { }
 
 void Pipeline::Pass::set_lighting(const Lighting *l)
@@ -242,6 +288,11 @@ void Pipeline::Pass::set_blend(const Blend *b)
        blend = b;
 }
 
+void Pipeline::Pass::set_clipping(const Clipping *c)
+{
+       clipping =c;
+}
+
 
 Pipeline::Slot::Slot(const Renderable *r):
        renderable(r)