X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Frender%2Fsequence.cpp;h=e953b4d6925944b10d872301ef35667a7904f857;hb=e9a898f315b5d1396f196d785913a283c30940f2;hp=9161c75146cc7552ac5363e21d69700a7ff5b564;hpb=45de0062950ad822ed0379ed4fe807d333e1b8f1;p=libs%2Fgl.git diff --git a/source/render/sequence.cpp b/source/render/sequence.cpp index 9161c751..e953b4d6 100644 --- a/source/render/sequence.cpp +++ b/source/render/sequence.cpp @@ -1,13 +1,11 @@ #include #include "blend.h" -#include "camera.h" +#include "error.h" #include "framebuffer.h" #include "lighting.h" #include "postprocessor.h" -#include "renderbuffer.h" #include "renderer.h" #include "sequence.h" -#include "tests.h" #include "texture2d.h" #include "view.h" @@ -16,97 +14,55 @@ using namespace std; namespace Msp { namespace GL { -Sequence::Sequence(unsigned w, unsigned h, bool d) +Sequence::Sequence(unsigned w, unsigned h, const FrameFormat &f) { - init(w, h); - hdr = d; + init(w, h, f); } -Sequence::Sequence(const View &view) +Sequence::Sequence(const View &view, const FrameFormat &f) { - init(view.get_width(), view.get_height()); + init(view.get_width(), view.get_height(), f); } -Sequence::Sequence(const Framebuffer &fbo) +Sequence::Sequence(const Framebuffer &fbo, const FrameFormat &f) { - init(fbo.get_width(), fbo.get_height()); + init(fbo.get_width(), fbo.get_height(), f); } -void Sequence::init(unsigned w, unsigned h) +void Sequence::init(unsigned w, unsigned h, const FrameFormat &f) { - camera = 0; width = w; height = h; - hdr = false; - alpha = false; - samples = 0; - target_ms = 0; - target[0] = 0; - target[1] = 0; -} - -Sequence::~Sequence() -{ - for(vector::iterator i=postproc.begin(); i!=postproc.end(); ++i) - if(i->owned) - delete i->postproc; - delete target[0]; - delete target[1]; - delete target_ms; -} - -void Sequence::set_hdr(bool h) -{ - if(h==hdr) - return; + target_format = f; - bool old_hdr = hdr; - hdr = h; - try + if(!target_format.empty()) { - create_targets(2); - } - catch(...) - { - hdr = old_hdr; - throw; - } -} - -void Sequence::set_alpha(bool a) -{ - if(a==alpha) - return; + FrameFormat postproc_fmt = target_format; + postproc_fmt.set_samples(1); + target[0] = new RenderTarget(width, height, postproc_fmt); + target[1] = new RenderTarget(width, height, postproc_fmt); - bool old_alpha = alpha; - alpha = a; - try - { - create_targets(2); + if(target_format.get_samples()>1) + target_ms = new RenderTarget(width, height, target_format); + else + target_ms = 0; } - catch(...) + else { - alpha = old_alpha; - throw; + target_ms = 0; + target[0] = 0; + target[1] = 0; } } -void Sequence::set_multisample(unsigned s) +Sequence::~Sequence() { - if(s==samples) - return; - - unsigned old_samples = samples; - samples = s; - try - { - create_targets(1); - } - catch(...) - { - samples = old_samples; - throw; - } + for(PostProcStep &p: postproc) + if(p.owned) + delete p.postproc; + delete target[0]; + delete target[1]; + delete target_ms; } Sequence::Step &Sequence::add_step(Tag tag, Renderable &r) @@ -127,31 +83,26 @@ void Sequence::add_postprocessor_owned(PostProcessor *pp) void Sequence::add_postprocessor(PostProcessor *pp, bool owned) { - postproc.push_back(PostProcStep(pp, owned)); - try + if(target_format.empty()) { - create_targets(0); - } - catch(...) - { - if(!owned) + if(owned) delete pp; - postproc.pop_back(); - throw; + throw invalid_operation("Sequence::add_postprocessor"); } + postproc.push_back(PostProcStep(pp, owned)); } void Sequence::setup_frame(Renderer &renderer) { - for(vector::const_iterator i=steps.begin(); i!=steps.end(); ++i) - if(Renderable *renderable = i->get_renderable()) + for(const Step &s: steps) + if(Renderable *renderable = s.get_renderable()) renderable->setup_frame(renderer); } void Sequence::finish_frame() { - for(vector::const_iterator i=steps.begin(); i!=steps.end(); ++i) - if(Renderable *renderable = i->get_renderable()) + for(const Step &s: steps) + if(Renderable *renderable = s.get_renderable()) renderable->finish_frame(); } @@ -160,114 +111,62 @@ void Sequence::render(Renderer &renderer, Tag tag) const if(tag.id) return; - 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()); + Renderer::Push _push(renderer); + + const Framebuffer *out_fbo = renderer.get_framebuffer(); if(target[0]) { - Framebuffer &fbo = (samples ? target_ms : target[0])->get_framebuffer(); - fbo.bind(); - fbo.clear(); + renderer.set_framebuffer(&(target_ms ? target_ms : target[0])->get_framebuffer()); + renderer.clear(); } - for(vector::const_iterator i=steps.begin(); i!=steps.end(); ++i) + for(const Step &s: steps) { - if(const DepthTest *dt = i->get_depth_test()) - dt->bind(); - else - DepthTest::unbind(); + Renderer::Push _push2(renderer); - if(const Blend *b = i->get_blend()) - b->bind(); - else - Blend::unbind(); + renderer.set_depth_test(&s.get_depth_test()); + renderer.set_stencil_test(&s.get_stencil_test()); + renderer.set_blend(&s.get_blend()); - if (const Lighting *lighting = i->get_lighting()) + if (const Lighting *lighting = s.get_lighting()) renderer.add_shader_data(lighting->get_shader_data()); - renderer.set_clipping(i->get_clipping()); + renderer.set_clipping(s.get_clipping()); - if(const Renderable *renderable = i->get_renderable()) - renderer.render(*renderable, i->get_tag()); + if(const Renderable *renderable = s.get_renderable()) + renderer.render(*renderable, s.get_tag()); } if(target[0]) { - DepthTest::unbind(); - Blend::unbind(); + if(target_ms) + renderer.resolve_multisample(target[0]->get_framebuffer(), COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT); - if(samples) - target[0]->blit_from(*target_ms); + renderer.set_depth_test(0); + renderer.set_stencil_test(0); + renderer.set_blend(0); 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); + renderer.set_framebuffer(i+1get_framebuffer() : out_fbo); + const Texture2D &color = target[j]->get_target_texture(COLOR_ATTACHMENT); + const Texture2D &depth = target[j]->get_target_texture(DEPTH_ATTACHMENT); postproc[i].postproc->render(renderer, color, depth); } } } -void Sequence::create_targets(unsigned recreate) -{ - if(recreate>=2) - { - delete target[0]; - delete target[1]; - target[0] = 0; - target[1] = 0; - } - if(recreate>=1) - { - delete target_ms; - target_ms = 0; - } - - PixelFormat color_pf = (hdr ? (alpha ? RGBA16F : RGB16F) : (alpha ? RGBA8 : RGB8)); - RenderTargetFormat fmt = (RENDER_COLOR,color_pf, RENDER_DEPTH); - 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 RenderTarget(width, height, samples, fmt); - -#ifdef DEBUG - if(!debug_name.empty()) - set_target_debug_names(); -#endif -} - void Sequence::set_debug_name(const string &name) { -#ifdef DEBUG - debug_name = name; - if(!name.empty()) - set_target_debug_names(); -#else - (void)name; -#endif -} - -void Sequence::set_target_debug_names() -{ #ifdef DEBUG for(unsigned i=0; i<2; ++i) if(target[i]) - target[i]->set_debug_name(format("%s [RT:%d]", debug_name, i)); + target[i]->set_debug_name(format("%s [RT:%d]", name, i)); if(target_ms) - target_ms->set_debug_name(debug_name+" [RT:ms]"); + target_ms->set_debug_name(name+" [RT:ms]"); +#else + (void)name; #endif } @@ -275,8 +174,6 @@ void Sequence::set_target_debug_names() Sequence::Step::Step(Tag t, Renderable *r): tag(t), lighting(0), - depth_test(0), - blend(0), clipping(0), renderable(r) { } @@ -286,19 +183,24 @@ void Sequence::Step::set_lighting(const Lighting *l) lighting = l; } -void Sequence::Step::set_depth_test(const DepthTest *d) +void Sequence::Step::set_depth_test(const DepthTest &dt) +{ + depth_test = dt; +} + +void Sequence::Step::set_stencil_test(const StencilTest &st) { - depth_test = d; + stencil_test = st; } -void Sequence::Step::set_blend(const Blend *b) +void Sequence::Step::set_blend(const Blend &b) { blend = b; } void Sequence::Step::set_clipping(const Clipping *c) { - clipping =c; + clipping = c; } } // namespace GL