X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fpipeline.cpp;h=dc1eba1d117cdcca5d6b190c53b3545ed31fd86c;hp=8757ea92e87e54a6a0e1b0dc33afe1c7f5b006e3;hb=3b159edbe4e80a2bc19c4c2fcd42cb996b9fbfe0;hpb=b07e3b2103c53e3cd6a4674eef481178ec64f2c7 diff --git a/source/pipeline.cpp b/source/pipeline.cpp index 8757ea92..dc1eba1d 100644 --- a/source/pipeline.cpp +++ b/source/pipeline.cpp @@ -21,36 +21,54 @@ 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), + in_frame(false) +{ + 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) { + if(h==hdr) + return; + + bool old_hdr= hdr; hdr = h; - if(!postproc.empty()) - create_fbos(); + 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; - if(!postproc.empty()) - create_fbos(); + try + { + create_targets(1); + } + catch(...) + { + samples = old_samples; + throw; + } } void Pipeline::set_camera(const Camera *c) @@ -58,21 +76,10 @@ void Pipeline::set_camera(const Camera *c) camera = c; } -PipelinePass &Pipeline::add_pass(const Tag &tag) -{ - PipelinePass &pass = insert_unique(passes, tag, PipelinePass())->second; - pass_order.push_back(tag); - return pass; -} - -PipelinePass &Pipeline::get_pass(const Tag &tag) +Pipeline::Pass &Pipeline::add_pass(const Tag &tag) { - return get_item(passes, tag); -} - -const PipelinePass &Pipeline::get_pass(const Tag &tag) const -{ - return get_item(passes, tag); + passes.push_back(Pass(tag, 0)); + return passes.back(); } void Pipeline::add_renderable(const Renderable &r) @@ -110,97 +117,180 @@ 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); - if(!fbo) - create_fbos(); + try { + create_targets(0); + } + catch(...) + { + postproc.pop_back(); + throw; } } -void Pipeline::render(Renderer &renderer, const Tag &tag) const +void Pipeline::setup_frame() const { - const PipelinePass &pass = get_pass(tag); - - Bind bind_depth_test(pass.depth_test); - Bind bind_blend(pass.blend); - Bind bind_lighting(pass.lighting); + 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) - if(i->passes.empty() || i->passes.count(tag)) - i->renderable->render(renderer, tag); + i->renderable->finish_frame(); } -void Pipeline::render_all() const +void Pipeline::render(const Tag &tag) const { - if(camera) - camera->apply(); + if(tag.id) + return; - if(fbo) + 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 *f = (fbo_ms ? fbo_ms : fbo); - f->bind(); - f->clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT); + Framebuffer &fbo = (samples ? target_ms->fbo : target[0]->fbo); + fbo.bind(); + fbo.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT); } - Renderer renderer(camera); - for(vector::const_iterator i=pass_order.begin(); i!=pass_order.end(); ++i) - render(renderer, *i); - if(fbo) + 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()); + } + + if(target[0]) { - if(fbo_ms) - fbo->blit_from(*fbo_ms, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false); - Framebuffer::unbind(); + BindRestore unbind_depth_test(static_cast(0)); + BindRestore unbind_blend(static_cast(0)); + + 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); + } } - // 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); + if(!was_in_frame) + finish_frame(); } -void Pipeline::create_fbos() +void Pipeline::create_targets(unsigned recreate) { - delete fbo; - delete color_buf; - delete depth_buf; + if(recreate>=2) + { + delete target[0]; + delete target[1]; + target[0] = 0; + target[1] = 0; + } + if(recreate>=1) + { + delete target_ms; + target_ms = 0; + } - delete fbo_ms; - fbo_ms = 0; - delete color_buf_ms; - color_buf_ms = 0; - delete depth_buf_ms; - depth_buf_ms = 0; + PixelFormat fmt = (hdr ? RGB16F : RGB); + 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); + } - fbo = new Framebuffer; + if(!target_ms && samples) + target_ms = new MultisampleTarget(width, height, samples, fmt); +} - 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); +Pipeline::Pass::Pass(const Tag &t, const Renderable *r): + tag(t), + lighting(0), + depth_test(0), + blend(0), + clipping(0), + renderable(r) +{ } - if(samples) - { - fbo_ms = new Framebuffer; +void Pipeline::Pass::set_lighting(const Lighting *l) +{ + lighting = l; +} - color_buf_ms = new Renderbuffer; - color_buf_ms->storage_multisample(samples, (hdr ? RGB16F : RGB), width, height); - fbo_ms->attach(COLOR_ATTACHMENT0, *color_buf_ms); +void Pipeline::Pass::set_depth_test(const DepthTest *d) +{ + depth_test = d; +} - depth_buf_ms = new Renderbuffer; - depth_buf_ms->storage_multisample(samples, DEPTH_COMPONENT, width, height); - fbo_ms->attach(DEPTH_ATTACHMENT, *depth_buf_ms); - } +void Pipeline::Pass::set_blend(const Blend *b) +{ + blend = b; +} + +void Pipeline::Pass::set_clipping(const Clipping *c) +{ + clipping =c; } @@ -208,5 +298,35 @@ 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); + + depth.storage_multisample(s, DEPTH_COMPONENT, w, h); + fbo.attach(DEPTH_ATTACHMENT, depth); + + fbo.require_complete(); +} + } // namespace GL } // namespace Msp