]> git.tdb.fi Git - libs/gl.git/blobdiff - source/pipeline.cpp
Drop Id tags and copyright notices from files
[libs/gl.git] / source / pipeline.cpp
index 54ef25a10bca8e59a123eb96322123e393595640..de98b2d383283d4822d1ed7e9b684ac60ed98e4b 100644 (file)
@@ -1,10 +1,3 @@
-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2009-2010  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
 #include "blend.h"
 #include "camera.h"
 #include "effect.h"
@@ -14,6 +7,7 @@ Distributed under the LGPL
 #include "pipeline.h"
 #include "postprocessor.h"
 #include "renderbuffer.h"
+#include "renderer.h"
 #include "tests.h"
 #include "texture2d.h"
 
@@ -27,9 +21,13 @@ Pipeline::Pipeline(unsigned w, unsigned h, bool d):
        width(w),
        height(h),
        hdr(d),
+       samples(0),
        fbo(0),
        color_buf(0),
-       depth_buf(0)
+       depth_buf(0),
+       fbo_ms(0),
+       color_buf_ms(0),
+       depth_buf_ms(0)
 { }
 
 Pipeline::~Pipeline()
@@ -39,6 +37,20 @@ Pipeline::~Pipeline()
        delete depth_buf;
 }
 
+void Pipeline::set_hdr(bool h)
+{
+       hdr = h;
+       if(!postproc.empty())
+               create_fbos();
+}
+
+void Pipeline::set_multisample(unsigned s)
+{
+       samples = s;
+       if(!postproc.empty())
+               create_fbos();
+}
+
 void Pipeline::set_camera(const Camera *c)
 {
        camera = c;
@@ -72,9 +84,39 @@ const PipelinePass &Pipeline::get_pass(const Tag &tag) const
 
 void Pipeline::add_renderable(const Renderable &r)
 {
+       for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
+               if(i->renderable==&r)
+               {
+                       i->passes.clear();
+                       return;
+               }
+
        renderables.push_back(&r);
 }
 
+void Pipeline::add_renderable_for_pass(const Renderable &r, const Tag &tag)
+{
+       for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
+               if(i->renderable==&r)
+               {
+                       i->passes.insert(tag);
+                       return;
+               }
+
+       renderables.push_back(&r);
+       renderables.back().passes.insert(tag);
+}
+
+void Pipeline::remove_renderable(const Renderable &r)
+{
+       for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
+               if(i->renderable==&r)
+               {
+                       renderables.erase(i);
+                       return;
+               }
+}
+
 void Pipeline::add_effect(Effect &e)
 {
        effects.push_back(&e);
@@ -84,22 +126,12 @@ void Pipeline::add_postprocessor(PostProcessor &pp)
 {
        postproc.push_back(&pp);
        if(!fbo)
+               create_fbos();
        {
-               fbo = new Framebuffer;
-               color_buf = new Texture2D;
-               color_buf->set_min_filter(NEAREST);
-               color_buf->set_mag_filter(NEAREST);
-               color_buf->storage((hdr ? RGB16F : RGB), width, height, 0);
-               color_buf->image(0, RGB, UNSIGNED_BYTE, 0);
-               fbo->attach(COLOR_ATTACHMENT0, *color_buf, 0);
-               depth_buf = new Renderbuffer;
-               depth_buf->storage(DEPTH_COMPONENT, width, height);
-               fbo->attach(DEPTH_ATTACHMENT, *depth_buf);
-               Framebuffer::unbind();
        }
 }
 
-void Pipeline::render(const Tag &tag) const
+void Pipeline::render(Renderer &renderer, const Tag &tag) const
 {
        const PipelinePass &pass = get_pass(tag);
 
@@ -110,8 +142,9 @@ void Pipeline::render(const Tag &tag) const
        for(vector<Effect *>::const_iterator i=pass.effects.begin(); i!=pass.effects.end(); ++i)
                (*i)->prepare();
 
-       for(vector<const Renderable *>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
-               (*i)->render(tag);
+       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);
 
        for(vector<Effect *>::const_iterator i=pass.effects.end(); i!=pass.effects.begin();)
                (*--i)->cleanup();
@@ -124,26 +157,80 @@ void Pipeline::render_all() const
 
        if(fbo)
        {
-               fbo->bind();
-               clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
+               Framebuffer *f = (fbo_ms ? fbo_ms : fbo);
+               f->bind();
+               f->clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
        }
 
        for(vector<Effect *>::const_iterator i=effects.begin(); i!=effects.end(); ++i)
                (*i)->prepare();
 
+       Renderer renderer(camera);
        for(vector<Tag>::const_iterator i=pass_order.begin(); i!=pass_order.end(); ++i)
-               render(*i);
+               render(renderer, *i);
 
        for(vector<Effect *>::const_iterator i=effects.end(); i!=effects.begin();)
                (*--i)->cleanup();
 
        if(fbo)
+       {
+               if(fbo_ms)
+                       fbo->blit_from(*fbo_ms, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false);
                Framebuffer::unbind();
+       }
 
        // 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);
+               (*i)->render(*color_buf, *depth_buf);
 }
 
+void Pipeline::create_fbos()
+{
+       delete fbo;
+       delete color_buf;
+       delete depth_buf;
+
+       delete fbo_ms;
+       fbo_ms = 0;
+       delete color_buf_ms;
+       color_buf_ms = 0;
+       delete depth_buf_ms;
+       depth_buf_ms = 0;
+
+       fbo = new Framebuffer;
+
+       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);
+
+       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(samples)
+       {
+               fbo_ms = new Framebuffer;
+
+               color_buf_ms = new Renderbuffer;
+               color_buf_ms->storage_multisample(samples, (hdr ? RGB16F : RGB), width, height);
+               fbo_ms->attach(COLOR_ATTACHMENT0, *color_buf_ms);
+
+               depth_buf_ms = new Renderbuffer;
+               depth_buf_ms->storage_multisample(samples, DEPTH_COMPONENT, width, height);
+               fbo_ms->attach(DEPTH_ATTACHMENT, *depth_buf_ms);
+       }
+}
+
+
+Pipeline::Slot::Slot(const Renderable *r):
+       renderable(r)
+{ }
+
 } // namespace GL
 } // namespace Msp