X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fpipeline.cpp;h=bfd7bc679c6d69c4432d8f3d52a83003c4b16157;hb=f48b68626a98c4a0b8991764d174eb57895e805f;hp=5294ffbd859d84f4c29ce7878dcd5a01a6fd376b;hpb=5687bb2b8cd7715b5a5c5bb1d75f42992a23a56c;p=libs%2Fgl.git diff --git a/source/pipeline.cpp b/source/pipeline.cpp index 5294ffbd..bfd7bc67 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,47 +20,41 @@ 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) +{ + 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; + hdr = h; + create_targets(true); } -PipelinePass &Pipeline::add_pass(const Tag &tag) +void Pipeline::set_multisample(unsigned s) { - if(passes.count(tag)) - throw KeyError("Pass already exists"); - - PipelinePass &pass = passes[tag]; - pass_order.push_back(tag); - return pass; + samples = s; + create_targets(false); } -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)); + return passes.back(); } void Pipeline::add_renderable(const Renderable &r) @@ -105,79 +92,107 @@ void Pipeline::remove_renderable(const Renderable &r) } } -void Pipeline::add_effect(Effect &e) -{ - effects.push_back(&e); -} - void Pipeline::add_postprocessor(PostProcessor &pp) { postproc.push_back(&pp); - if(!fbo) - { - 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); - } + create_targets(false); } 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; - Bind bind_depth_test(pass.depth_test); - Bind bind_blend(pass.blend); - Bind bind_lighting(pass.lighting); + if(target[0]) + { + Framebuffer &fbo = (samples ? target_ms->fbo : target[0]->fbo); + // XXX exception safety + fbo.bind(); + fbo.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT); + } + + 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()); + + for(vector::const_iterator j=renderables.begin(); j!=renderables.end(); ++j) + if(j->passes.empty() || j->passes.count(i->get_tag())) + j->renderable->render(renderer, i->get_tag()); + } - for(vector::const_iterator i=pass.effects.begin(); i!=pass.effects.end(); ++i) - (*i)->prepare(); + if(target[0]) + { + if(samples) + target[0]->fbo.blit_from(target_ms->fbo, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false); - for(vector::const_iterator i=renderables.begin(); i!=renderables.end(); ++i) - if(i->passes.empty() || i->passes.count(tag)) - i->renderable->render(tag); + for(unsigned i=0; ifbo.bind(); + else + Framebuffer::unbind(); + postproc[i]->render(target[j]->color, target[j]->depth); + } - for(vector::const_iterator i=pass.effects.end(); i!=pass.effects.begin();) - (*--i)->cleanup(); + Framebuffer::unbind(); + } } -void Pipeline::render_all() const +void Pipeline::create_targets(bool recreate) { - if(camera) - camera->apply(); - - if(fbo) + if(recreate) { - fbo->bind(); - fbo->clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT); + delete target[0]; + delete target[1]; + delete target_ms; + target[0] = 0; + target[1] = 0; + target_ms = 0; } - for(vector::const_iterator i=effects.begin(); i!=effects.end(); ++i) - (*i)->prepare(); + 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); - for(vector::const_iterator i=pass_order.begin(); i!=pass_order.end(); ++i) - render(*i); + if(!target_ms && samples) + target_ms = new MultisampleTarget(width, height, samples, fmt); +} - for(vector::const_iterator i=effects.end(); i!=effects.begin();) - (*--i)->cleanup(); - if(fbo) - Framebuffer::unbind(); +Pipeline::Pass::Pass(const Tag &t): + tag(t), + lighting(0), + depth_test(0), + blend(0) +{ } - // 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::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; } @@ -185,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