]> git.tdb.fi Git - libs/gl.git/blobdiff - source/pipeline.cpp
Notify Renderables about the start and end of a frame
[libs/gl.git] / source / pipeline.cpp
index ce15597080618114fc719e1e332732f20809cb76..466aa23c69b6b3107457817f8efea8f33aa829ac 100644 (file)
@@ -21,36 +21,30 @@ Pipeline::Pipeline(unsigned w, unsigned h, bool d):
        height(h),
        hdr(d),
        samples(0),
-       fbo(0),
-       color_buf(0),
-       depth_buf(0),
-       fbo_ms(0),
-       color_buf_ms(0),
-       depth_buf_ms(0)
-{ }
+       target_ms(0),
+       in_frame(false)
+{
+       target[0] = 0;
+       target[1] = 0;
+}
 
 Pipeline::~Pipeline()
 {
-       delete fbo;
-       delete color_buf;
-       delete depth_buf;
-       delete fbo_ms;
-       delete color_buf_ms;
-       delete depth_buf_ms;
+       delete target[0];
+       delete target[1];
+       delete target_ms;
 }
 
 void Pipeline::set_hdr(bool h)
 {
        hdr = h;
-       if(!postproc.empty())
-               create_fbos();
+       create_targets(true);
 }
 
 void Pipeline::set_multisample(unsigned s)
 {
        samples = s;
-       if(!postproc.empty())
-               create_fbos();
+       create_targets(false);
 }
 
 void Pipeline::set_camera(const Camera *c)
@@ -60,19 +54,8 @@ void Pipeline::set_camera(const Camera *c)
 
 Pipeline::Pass &Pipeline::add_pass(const Tag &tag)
 {
-       Pass &pass = insert_unique(passes, tag, Pass())->second;
-       pass_order.push_back(tag);
-       return pass;
-}
-
-Pipeline::Pass &Pipeline::get_pass(const Tag &tag)
-{
-       return get_item(passes, tag);
-}
-
-const Pipeline::Pass &Pipeline::get_pass(const Tag &tag) const
-{
-       return get_item(passes, tag);
+       passes.push_back(Pass(tag));
+       return passes.back();
 }
 
 void Pipeline::add_renderable(const Renderable &r)
@@ -113,98 +96,107 @@ void Pipeline::remove_renderable(const Renderable &r)
 void Pipeline::add_postprocessor(PostProcessor &pp)
 {
        postproc.push_back(&pp);
-       if(!fbo)
-               create_fbos();
-       {
-       }
+       create_targets(false);
 }
 
-void Pipeline::render(Renderer &renderer, const Tag &tag) const
+void Pipeline::setup_frame() const
 {
-       const Pass &pass = get_pass(tag);
-
-       Bind bind_depth_test(pass.get_depth_test());
-       Bind bind_blend(pass.get_blend());
-       Bind bind_lighting(pass.get_lighting());
-
+       in_frame = true;
        for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
-               if(i->passes.empty() || i->passes.count(tag))
-                       i->renderable->render(renderer, tag);
+               i->renderable->setup_frame();
 }
 
-void Pipeline::render_all() const
+void Pipeline::finish_frame() const
 {
-       if(camera)
-               camera->apply();
+       in_frame = false;
+       for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
+               i->renderable->finish_frame();
+}
 
-       if(fbo)
-       {
-               Framebuffer *f = (fbo_ms ? fbo_ms : fbo);
-               f->bind();
-               f->clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
-       }
+void Pipeline::render(const Tag &tag) const
+{
+       if(tag.id)
+               return;
 
        Renderer renderer(camera);
-       for(vector<Tag>::const_iterator i=pass_order.begin(); i!=pass_order.end(); ++i)
-               render(renderer, *i);
+       render(renderer, tag);
+}
+
+void Pipeline::render(Renderer &renderer, const Tag &tag) const
+{
+       if(tag.id)
+               return;
+
+       bool was_in_frame = in_frame;
+       if(!in_frame)
+               setup_frame();
 
-       if(fbo)
+       if(target[0])
        {
-               if(fbo_ms)
-                       fbo->blit_from(*fbo_ms, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false);
-               Framebuffer::unbind();
+               Framebuffer &fbo = (samples ? target_ms->fbo : target[0]->fbo);
+               // XXX exception safety
+               fbo.bind();
+               fbo.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
        }
 
-       // XXX Need two color buffer textures to handle multiple post-processors correctly
-       for(vector<PostProcessor *>::const_iterator i=postproc.begin(); i!=postproc.end(); ++i)
-               (*i)->render(*color_buf, *depth_buf);
-}
+       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());
+               Bind bind_lighting(i->get_lighting());
 
-void Pipeline::create_fbos()
-{
-       delete fbo;
-       delete color_buf;
-       delete depth_buf;
+               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());
+       }
 
-       delete fbo_ms;
-       fbo_ms = 0;
-       delete color_buf_ms;
-       color_buf_ms = 0;
-       delete depth_buf_ms;
-       depth_buf_ms = 0;
+       if(target[0])
+       {
+               if(samples)
+                       target[0]->fbo.blit_from(target_ms->fbo, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false);
 
-       fbo = new Framebuffer;
+               for(unsigned i=0; i<postproc.size(); ++i)
+               {
+                       unsigned j = i%2;
+                       if(i+1<postproc.size())
+                               target[1-j]->fbo.bind();
+                       else
+                               Framebuffer::unbind();
+                       postproc[i]->render(target[j]->color, target[j]->depth);
+               }
 
-       color_buf = new Texture2D;
-       color_buf->set_min_filter(NEAREST);
-       color_buf->set_mag_filter(NEAREST);
-       color_buf->set_wrap(CLAMP_TO_EDGE);
-       color_buf->storage((hdr ? RGB16F : RGB), width, height);
-       fbo->attach(COLOR_ATTACHMENT0, *color_buf, 0);
+               Framebuffer::unbind();
+       }
 
-       depth_buf = new Texture2D;
-       depth_buf->set_min_filter(NEAREST);
-       depth_buf->set_mag_filter(NEAREST);
-       depth_buf->set_wrap(CLAMP_TO_EDGE);
-       depth_buf->storage(DEPTH_COMPONENT, width, height);
-       fbo->attach(DEPTH_ATTACHMENT, *depth_buf, 0);
+       if(!was_in_frame)
+               finish_frame();
+}
 
-       if(samples)
+void Pipeline::create_targets(bool recreate)
+{
+       if(recreate)
        {
-               fbo_ms = new Framebuffer;
+               delete target[0];
+               delete target[1];
+               delete target_ms;
+               target[0] = 0;
+               target[1] = 0;
+               target_ms = 0;
+       }
 
-               color_buf_ms = new Renderbuffer;
-               color_buf_ms->storage_multisample(samples, (hdr ? RGB16F : RGB), width, height);
-               fbo_ms->attach(COLOR_ATTACHMENT0, *color_buf_ms);
+       PixelFormat fmt = (hdr ? RGB16F : RGB);
+       if(!target[0])
+               target[0] = new RenderTarget(width, height, fmt);
+       if(!target[1] && postproc.size()>1)
+               target[1] = new RenderTarget(width, height, fmt);
 
-               depth_buf_ms = new Renderbuffer;
-               depth_buf_ms->storage_multisample(samples, DEPTH_COMPONENT, width, height);
-               fbo_ms->attach(DEPTH_ATTACHMENT, *depth_buf_ms);
-       }
+       if(!target_ms && samples)
+               target_ms = new MultisampleTarget(width, height, samples, fmt);
 }
 
 
-Pipeline::Pass::Pass():
+Pipeline::Pass::Pass(const Tag &t):
+       tag(t),
        lighting(0),
        depth_test(0),
        blend(0)
@@ -230,5 +222,31 @@ Pipeline::Slot::Slot(const Renderable *r):
        renderable(r)
 { }
 
+
+Pipeline::RenderTarget::RenderTarget(unsigned w, unsigned h, PixelFormat f)
+{
+       color.set_min_filter(NEAREST);
+       color.set_mag_filter(NEAREST);
+       color.set_wrap(CLAMP_TO_EDGE);
+       color.storage(f, w, h);
+       fbo.attach(COLOR_ATTACHMENT0, color, 0);
+
+       depth.set_min_filter(NEAREST);
+       depth.set_mag_filter(NEAREST);
+       depth.set_wrap(CLAMP_TO_EDGE);
+       depth.storage(DEPTH_COMPONENT, w, h);
+       fbo.attach(DEPTH_ATTACHMENT, depth, 0);
+}
+
+
+Pipeline::MultisampleTarget::MultisampleTarget(unsigned w, unsigned h, unsigned s, PixelFormat f)
+{
+       color.storage_multisample(s, f, w, h);
+       fbo.attach(COLOR_ATTACHMENT0, color);
+
+       depth.storage_multisample(s, DEPTH_COMPONENT, w, h);
+       fbo.attach(DEPTH_ATTACHMENT, depth);
+}
+
 } // namespace GL
 } // namespace Msp