X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fpipeline.cpp;h=e468825748a640ed53cab215c6fc0ebee8c14fb3;hb=14e5bc2c02ef8cc468d5727065ad732bb422a89c;hp=2dd178f247a53b97d26f74bbfe526292e21ae4d2;hpb=2e6a73a93eac0a18063ec675a24a8e6eeeb80a0c;p=libs%2Fgl.git diff --git a/source/pipeline.cpp b/source/pipeline.cpp index 2dd178f2..e4688257 100644 --- a/source/pipeline.cpp +++ b/source/pipeline.cpp @@ -21,7 +21,8 @@ Pipeline::Pipeline(unsigned w, unsigned h, bool d): height(h), hdr(d), samples(0), - target_ms(0) + target_ms(0), + in_frame(false) { target[0] = 0; target[1] = 0; @@ -36,14 +37,38 @@ Pipeline::~Pipeline() void Pipeline::set_hdr(bool h) { + if(h==hdr) + return; + + bool old_hdr= hdr; hdr = h; - create_targets(true); + try + { + create_targets(2); + } + catch(...) + { + hdr = old_hdr; + throw; + } } void Pipeline::set_multisample(unsigned s) { + if(s==samples) + return; + + unsigned old_samples = samples; samples = s; - create_targets(false); + try + { + create_targets(1); + } + catch(...) + { + samples = old_samples; + throw; + } } void Pipeline::set_camera(const Camera *c) @@ -53,7 +78,7 @@ void Pipeline::set_camera(const Camera *c) Pipeline::Pass &Pipeline::add_pass(const Tag &tag) { - passes.push_back(Pass(tag)); + passes.push_back(Pass(tag, 0)); return passes.back(); } @@ -92,10 +117,44 @@ void Pipeline::remove_renderable(const Renderable &r) } } +Pipeline::Pass &Pipeline::add_pass(const Tag &tag, const Renderable &r) +{ + passes.push_back(Pass(tag, &r)); + return passes.back(); +} + void Pipeline::add_postprocessor(PostProcessor &pp) { postproc.push_back(&pp); - create_targets(false); + try + { + 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 @@ -112,19 +171,41 @@ 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); - // 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()); + 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())) @@ -133,6 +214,9 @@ void Pipeline::render(Renderer &renderer, const Tag &tag) const if(target[0]) { + DepthTest::unbind(); + Blend::unbind(); + if(samples) target[0]->fbo.blit_from(target_ms->fbo, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false); @@ -142,42 +226,51 @@ void Pipeline::render(Renderer &renderer, const Tag &tag) const if(i+1fbo.bind(); else - Framebuffer::unbind(); - postproc[i]->render(target[j]->color, target[j]->depth); + out_fbo->bind(); + postproc[i]->render(renderer, target[j]->color, target[j]->depth); } - - Framebuffer::unbind(); } + + if(!was_in_frame) + finish_frame(); } -void Pipeline::create_targets(bool recreate) +void Pipeline::create_targets(unsigned recreate) { - if(recreate) + if(recreate>=2) { delete target[0]; delete target[1]; - delete target_ms; target[0] = 0; target[1] = 0; + } + if(recreate>=1) + { + delete target_ms; target_ms = 0; } 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); + if(!postproc.empty() || samples) + { + if(!target[0]) + target[0] = new RenderTarget(width, height, fmt); + if(!target[1] && postproc.size()>1) + target[1] = new RenderTarget(width, height, fmt); + } if(!target_ms && samples) target_ms = new MultisampleTarget(width, height, samples, fmt); } -Pipeline::Pass::Pass(const Tag &t): +Pipeline::Pass::Pass(const Tag &t, const Renderable *r): tag(t), lighting(0), depth_test(0), - blend(0) + blend(0), + clipping(0), + renderable(r) { } void Pipeline::Pass::set_lighting(const Lighting *l) @@ -195,6 +288,11 @@ void Pipeline::Pass::set_blend(const Blend *b) blend = b; } +void Pipeline::Pass::set_clipping(const Clipping *c) +{ + clipping =c; +} + Pipeline::Slot::Slot(const Renderable *r): renderable(r) @@ -214,6 +312,8 @@ Pipeline::RenderTarget::RenderTarget(unsigned w, unsigned h, PixelFormat f) depth.set_wrap(CLAMP_TO_EDGE); depth.storage(DEPTH_COMPONENT, w, h); fbo.attach(DEPTH_ATTACHMENT, depth, 0); + + fbo.require_complete(); } @@ -224,6 +324,8 @@ Pipeline::MultisampleTarget::MultisampleTarget(unsigned w, unsigned h, unsigned depth.storage_multisample(s, DEPTH_COMPONENT, w, h); fbo.attach(DEPTH_ATTACHMENT, depth); + + fbo.require_complete(); } } // namespace GL