From: Mikko Rasa Date: Mon, 20 Aug 2012 20:55:59 +0000 (+0300) Subject: Use two FBOs in Pipeline when necessary X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=commitdiff_plain;h=f48b68626a98c4a0b8991764d174eb57895e805f Use two FBOs in Pipeline when necessary --- diff --git a/source/pipeline.cpp b/source/pipeline.cpp index 67ddf656..bfd7bc67 100644 --- a/source/pipeline.cpp +++ b/source/pipeline.cpp @@ -21,36 +21,29 @@ 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) +{ + 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) @@ -102,10 +95,7 @@ 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(const Tag &tag) const @@ -122,12 +112,12 @@ void Pipeline::render(Renderer &renderer, const Tag &tag) const if(tag.id) return; - if(fbo) + if(target[0]) { - Framebuffer *f = (fbo_ms ? fbo_ms : fbo); + Framebuffer &fbo = (samples ? target_ms->fbo : target[0]->fbo); // XXX exception safety - f->bind(); - f->clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT); + fbo.bind(); + fbo.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT); } for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i) @@ -141,59 +131,45 @@ void Pipeline::render(Renderer &renderer, const Tag &tag) const j->renderable->render(renderer, i->get_tag()); } - if(fbo) + if(target[0]) { - if(fbo_ms) - fbo->blit_from(*fbo_ms, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false); + if(samples) + target[0]->fbo.blit_from(target_ms->fbo, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false); + + for(unsigned i=0; ifbo.bind(); + else + Framebuffer::unbind(); + postproc[i]->render(target[j]->color, target[j]->depth); + } + Framebuffer::unbind(); } - - // XXX Need two color buffer textures to handle multiple post-processors correctly - for(vector::const_iterator i=postproc.begin(); i!=postproc.end(); ++i) - (*i)->render(*color_buf, *depth_buf); } -void Pipeline::create_fbos() +void Pipeline::create_targets(bool recreate) { - 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) + 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); } @@ -224,5 +200,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 diff --git a/source/pipeline.h b/source/pipeline.h index c14245fa..d39bce5a 100644 --- a/source/pipeline.h +++ b/source/pipeline.h @@ -3,7 +3,10 @@ #include #include +#include "framebuffer.h" #include "renderable.h" +#include "renderbuffer.h" +#include "texture2d.h" namespace Msp { namespace GL { @@ -11,11 +14,8 @@ namespace GL { class Blend; class Camera; class DepthTest; -class Framebuffer; class Lighting; class PostProcessor; -class Renderbuffer; -class Texture2D; class Pipeline: public Renderable { @@ -50,6 +50,24 @@ private: Slot(const Renderable *); }; + struct RenderTarget + { + Framebuffer fbo; + Texture2D color; + Texture2D depth; + + RenderTarget(unsigned, unsigned, PixelFormat); + }; + + struct MultisampleTarget + { + Framebuffer fbo; + Renderbuffer color; + Renderbuffer depth; + + MultisampleTarget(unsigned, unsigned, unsigned, PixelFormat); + }; + typedef std::list PassList; PassList passes; @@ -60,12 +78,8 @@ private: unsigned height; bool hdr; unsigned samples; - Framebuffer *fbo; - Texture2D *color_buf; - Texture2D *depth_buf; - Framebuffer *fbo_ms; - Renderbuffer *color_buf_ms; - Renderbuffer *depth_buf_ms; + RenderTarget *target[2]; + MultisampleTarget *target_ms; public: Pipeline(unsigned, unsigned, bool = false); @@ -86,7 +100,7 @@ public: virtual void render(Renderer &, const Tag &tag = Tag()) const; private: - void create_fbos(); + void create_targets(bool); }; } // namespace GL