X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fpipeline.cpp;h=e468825748a640ed53cab215c6fc0ebee8c14fb3;hb=14e5bc2c02ef8cc468d5727065ad732bb422a89c;hp=517037a0029785198fd724c44e5c92943f11ee82;hpb=3663bed18358a2399b2a8a8f7779d85e0ed81bd0;p=libs%2Fgl.git diff --git a/source/pipeline.cpp b/source/pipeline.cpp index 517037a0..e4688257 100644 --- a/source/pipeline.cpp +++ b/source/pipeline.cpp @@ -1,19 +1,12 @@ -/* $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" @@ -27,121 +20,312 @@ Pipeline::Pipeline(unsigned w, unsigned h, bool d): width(w), height(h), hdr(d), - fbo(0), - color_buf(0), - depth_buf(0) -{ } + samples(0), + target_ms(0), + in_frame(false) +{ + 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; - PipelinePass &pass = passes[tag]; - pass_order.push_back(tag); - return pass; + unsigned old_samples = samples; + samples = s; + try + { + create_targets(1); + } + catch(...) + { + samples = old_samples; + throw; + } } -PipelinePass &Pipeline::get_pass(const Tag &tag) +void Pipeline::set_camera(const Camera *c) { - PassMap::iterator i = passes.find(tag); - if(i==passes.end()) - throw KeyError("Unknown pass"); - return i->second; + camera = c; } -const PipelinePass &Pipeline::get_pass(const Tag &tag) const +Pipeline::Pass &Pipeline::add_pass(const Tag &tag) { - PassMap::const_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(); } void Pipeline::add_renderable(const Renderable &r) { + 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_for_pass(const 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::remove_renderable(const Renderable &r) +{ + for(vector::iterator i=renderables.begin(); i!=renderables.end(); ++i) + if(i->renderable==&r) + { + renderables.erase(i); + return; + } } -void Pipeline::add_effect(Effect &e) +Pipeline::Pass &Pipeline::add_pass(const Tag &tag, const Renderable &r) { - effects.push_back(&e); + passes.push_back(Pass(tag, &r)); + return passes.back(); } void Pipeline::add_postprocessor(PostProcessor &pp) { postproc.push_back(&pp); - if(!fbo) + try { - 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); + 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::const_iterator i=renderables.begin(); i!=renderables.end(); ++i) + i->renderable->setup_frame(); +} + +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::const_iterator i=renderables.begin(); i!=renderables.end(); ++i) + i->renderable->finish_frame(); } void Pipeline::render(const Tag &tag) const { - const PipelinePass &pass = get_pass(tag); + if(tag.id) + return; + + Renderer renderer(camera); + 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(); + + const Framebuffer *out_fbo = Framebuffer::current(); + // 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]) + { + Framebuffer &fbo = (samples ? target_ms->fbo : target[0]->fbo); + fbo.bind(); + fbo.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT); + } + - Bind bind_depth_test(pass.depth_test); - Bind bind_blend(pass.blend); - Bind bind_lighting(pass.lighting); + 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(); - for(vector::const_iterator i=pass.effects.begin(); i!=pass.effects.end(); ++i) - (*i)->prepare(); + renderer.set_lighting(i->get_lighting()); + renderer.set_clipping(i->get_clipping()); - for(vector::const_iterator i=renderables.begin(); i!=renderables.end(); ++i) - (*i)->render(tag); + if(const Renderable *renderable = i->get_renderable()) + renderer.render(*renderable, i->get_tag()); - for(vector::const_iterator i=pass.effects.end(); i!=pass.effects.begin();) - (*--i)->cleanup(); + 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()); + } + + if(target[0]) + { + DepthTest::unbind(); + Blend::unbind(); + + if(samples) + target[0]->fbo.blit_from(target_ms->fbo, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false); + + for(unsigned i=0; ifbo.bind(); + else + out_fbo->bind(); + postproc[i]->render(renderer, target[j]->color, target[j]->depth); + } + } + + if(!was_in_frame) + finish_frame(); } -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 fmt = (hdr ? RGB16F : RGB); + 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 MultisampleTarget(width, height, samples, fmt); +} + + +Pipeline::Pass::Pass(const Tag &t, const Renderable *r): + tag(t), + lighting(0), + depth_test(0), + blend(0), + clipping(0), + renderable(r) +{ } + +void Pipeline::Pass::set_lighting(const Lighting *l) +{ + lighting = l; +} + +void Pipeline::Pass::set_depth_test(const DepthTest *d) +{ + depth_test = d; +} + +void Pipeline::Pass::set_blend(const Blend *b) +{ + blend = b; +} + +void Pipeline::Pass::set_clipping(const Clipping *c) +{ + clipping =c; +} - for(vector::const_iterator i=pass_order.begin(); i!=pass_order.end(); ++i) - render(*i); - for(vector::const_iterator i=effects.end(); i!=effects.begin();) - (*--i)->cleanup(); +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); + + fbo.require_complete(); +} + + +Pipeline::MultisampleTarget::MultisampleTarget(unsigned w, unsigned h, unsigned s, PixelFormat f) +{ + color.storage_multisample(s, f, w, h); + fbo.attach(COLOR_ATTACHMENT0, color); - if(fbo) - Framebuffer::unbind(); + depth.storage_multisample(s, DEPTH_COMPONENT, w, h); + fbo.attach(DEPTH_ATTACHMENT, depth); - // 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); + fbo.require_complete(); } } // namespace GL