X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fpipeline.cpp;h=27cc3e3c63cc9d274e3573c7da915b21b846899d;hp=517037a0029785198fd724c44e5c92943f11ee82;hb=bb162b9edd4b8c0e9faeed75da4148f5b9735450;hpb=3663bed18358a2399b2a8a8f7779d85e0ed81bd0 diff --git a/source/pipeline.cpp b/source/pipeline.cpp index 517037a0..27cc3e3c 100644 --- a/source/pipeline.cpp +++ b/source/pipeline.cpp @@ -1,148 +1,317 @@ -/* $Id$ - -This file is part of libmspgl -Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - +#include #include "blend.h" #include "camera.h" -#include "effect.h" -#include "except.h" #include "framebuffer.h" #include "lighting.h" #include "pipeline.h" #include "postprocessor.h" #include "renderbuffer.h" +#include "renderer.h" #include "tests.h" #include "texture2d.h" +#include "view.h" using namespace std; namespace Msp { namespace GL { -Pipeline::Pipeline(unsigned w, unsigned h, bool d): - camera(0), - width(w), - height(h), - hdr(d), - fbo(0), - color_buf(0), - depth_buf(0) -{ } +Pipeline::Pipeline(unsigned w, unsigned h, bool d) +{ + init(w, h); + hdr = d; +} + +Pipeline::Pipeline(const View &view) +{ + init(view.get_width(), view.get_height()); +} + +void Pipeline::init(unsigned w, unsigned h) +{ + camera = 0; + width = w; + height = h; + hdr = false; + samples = 0; + target_ms = 0; + target[0] = 0; + target[1] = 0; +} Pipeline::~Pipeline() { - delete fbo; - delete color_buf; - delete depth_buf; + delete target[0]; + delete target[1]; + delete target_ms; } -void Pipeline::set_camera(const Camera *c) +void Pipeline::set_hdr(bool h) { - camera = c; + if(h==hdr) + return; + + bool old_hdr= hdr; + hdr = h; + try + { + create_targets(2); + } + catch(...) + { + hdr = old_hdr; + throw; + } } -PipelinePass &Pipeline::add_pass(const Tag &tag) +void Pipeline::set_multisample(unsigned s) { - if(passes.count(tag)) - throw KeyError("Pass already exists"); + if(s==samples) + return; + + unsigned old_samples = samples; + samples = s; + try + { + create_targets(1); + } + catch(...) + { + samples = old_samples; + throw; + } +} - PipelinePass &pass = passes[tag]; - pass_order.push_back(tag); - return pass; +void Pipeline::set_camera(const Camera *c) +{ + camera = c; } -PipelinePass &Pipeline::get_pass(const Tag &tag) +Pipeline::Pass &Pipeline::add_pass(const Tag &tag) { - PassMap::iterator i = passes.find(tag); - if(i==passes.end()) - throw KeyError("Unknown pass"); - return i->second; + passes.push_back(Pass(tag, 0)); + return passes.back(); } -const PipelinePass &Pipeline::get_pass(const Tag &tag) const +void Pipeline::add_renderable(Renderable &r) { - PassMap::const_iterator i = passes.find(tag); - if(i==passes.end()) - throw KeyError("Unknown pass"); - return i->second; + for(vector::iterator i=renderables.begin(); i!=renderables.end(); ++i) + if(i->renderable==&r) + { + i->passes.clear(); + return; + } + + renderables.push_back(&r); } -void Pipeline::add_renderable(const Renderable &r) +void Pipeline::add_renderable_for_pass(Renderable &r, const Tag &tag) { + for(vector::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::add_effect(Effect &e) +void Pipeline::remove_renderable(Renderable &r) { - effects.push_back(&e); + for(vector::iterator i=renderables.begin(); i!=renderables.end(); ++i) + if(i->renderable==&r) + { + renderables.erase(i); + return; + } +} + +Pipeline::Pass &Pipeline::add_pass(const Tag &tag, Renderable &r) +{ + passes.push_back(Pass(tag, &r)); + return passes.back(); } void Pipeline::add_postprocessor(PostProcessor &pp) { - postproc.push_back(&pp); - if(!fbo) + add_postprocessor(&pp, true); +} + +void Pipeline::add_postprocessor_owned(PostProcessor *pp) +{ + add_postprocessor(pp, false); +} + +void Pipeline::add_postprocessor(PostProcessor *pp, bool keep) +{ + postproc.push_back(pp); + if(keep) + postproc.back().keep(); + try + { + create_targets(0); + } + catch(...) { - 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); + postproc.pop_back(); + throw; } } -void Pipeline::render(const Tag &tag) const +void Pipeline::setup_frame(Renderer &renderer) +{ + for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i) + if(Renderable *renderable = i->get_renderable()) + renderable->setup_frame(renderer); + for(vector::const_iterator i=renderables.begin(); i!=renderables.end(); ++i) + i->renderable->setup_frame(renderer); +} + +void Pipeline::finish_frame() +{ + for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i) + if(Renderable *renderable = i->get_renderable()) + renderable->finish_frame(); + for(vector::const_iterator i=renderables.begin(); i!=renderables.end(); ++i) + i->renderable->finish_frame(); +} + +void Pipeline::render() +{ + Renderer renderer(camera); + setup_frame(renderer); + render(renderer); + finish_frame(); +} + +void Pipeline::render(Renderer &renderer, const Tag &tag) const { - const PipelinePass &pass = get_pass(tag); + if(tag.id) + return; - Bind bind_depth_test(pass.depth_test); - Bind bind_blend(pass.blend); - Bind bind_lighting(pass.lighting); + 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]) + { + Framebuffer &fbo = (samples ? target_ms : target[0])->get_framebuffer(); + fbo.bind(); + fbo.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT); + } + + for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i) + { + 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::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()); + } - for(vector::const_iterator i=pass.effects.begin(); i!=pass.effects.end(); ++i) - (*i)->prepare(); + if(target[0]) + { + DepthTest::unbind(); + Blend::unbind(); - for(vector::const_iterator i=renderables.begin(); i!=renderables.end(); ++i) - (*i)->render(tag); + if(samples) + target[0]->blit_from(*target_ms); - for(vector::const_iterator i=pass.effects.end(); i!=pass.effects.begin();) - (*--i)->cleanup(); + for(unsigned i=0; iget_framebuffer().bind(); + else + 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); + } + } } -void Pipeline::render_all() const +void Pipeline::create_targets(unsigned recreate) { - if(camera) - camera->apply(); + if(recreate>=2) + { + delete target[0]; + delete target[1]; + target[0] = 0; + target[1] = 0; + } + if(recreate>=1) + { + delete target_ms; + target_ms = 0; + } - if(fbo) + PixelFormat color_pf = (hdr ? RGB16F : RGB); + RenderTargetFormat fmt = (RENDER_COLOR,color_pf, RENDER_DEPTH); + if(!postproc.empty() || samples) { - fbo->bind(); - fbo->clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT); + if(!target[0]) + target[0] = new RenderTarget(width, height, fmt); + if(!target[1] && postproc.size()>1) + target[1] = new RenderTarget(width, height, fmt); } - for(vector::const_iterator i=effects.begin(); i!=effects.end(); ++i) - (*i)->prepare(); + if(!target_ms && samples) + target_ms = new RenderTarget(width, height, samples, fmt); +} + + +Pipeline::Pass::Pass(const Tag &t, Renderable *r): + tag(t), + lighting(0), + depth_test(0), + blend(0), + clipping(0), + renderable(r) +{ } - for(vector::const_iterator i=pass_order.begin(); i!=pass_order.end(); ++i) - render(*i); +void Pipeline::Pass::set_lighting(const Lighting *l) +{ + lighting = l; +} - for(vector::const_iterator i=effects.end(); i!=effects.begin();) - (*--i)->cleanup(); +void Pipeline::Pass::set_depth_test(const DepthTest *d) +{ + depth_test = d; +} - if(fbo) - Framebuffer::unbind(); +void Pipeline::Pass::set_blend(const Blend *b) +{ + blend = b; +} - // 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); +void Pipeline::Pass::set_clipping(const Clipping *c) +{ + clipping =c; } + +Pipeline::Slot::Slot(Renderable *r): + renderable(r) +{ } + } // namespace GL } // namespace Msp