1 #include <msp/core/maputils.h>
4 #include "framebuffer.h"
6 #include "postprocessor.h"
16 Sequence::Sequence(unsigned w, unsigned h, const FrameFormat &f):
21 if(!target_format.empty())
23 FrameFormat postproc_fmt = target_format;
24 postproc_fmt.set_samples(1);
25 target[0] = new RenderTarget(width, height, postproc_fmt);
26 target[1] = new RenderTarget(width, height, postproc_fmt);
28 if(target_format.get_samples()>1)
29 target_ms = new RenderTarget(width, height, target_format);
43 for(PostProcStep &p: postproc)
51 Sequence::Step &Sequence::add_step(Tag tag, Renderable &r)
53 steps.push_back(Step(tag, &r));
57 void Sequence::add_postprocessor(PostProcessor &pp)
59 add_postprocessor(&pp, false);
62 void Sequence::add_postprocessor_owned(PostProcessor *pp)
64 add_postprocessor(pp, true);
67 void Sequence::add_postprocessor(PostProcessor *pp, bool owned)
69 if(target_format.empty())
73 throw invalid_operation("Sequence::add_postprocessor");
75 postproc.push_back(PostProcStep(pp, owned));
78 void Sequence::setup_frame(Renderer &renderer)
80 for(const Step &s: steps)
81 if(Renderable *renderable = s.get_renderable())
82 renderable->setup_frame(renderer);
85 void Sequence::finish_frame()
87 for(const Step &s: steps)
88 if(Renderable *renderable = s.get_renderable())
89 renderable->finish_frame();
92 void Sequence::render(Renderer &renderer, Tag tag) const
97 Renderer::Push _push(renderer);
99 const Framebuffer *out_fbo = renderer.get_framebuffer();
103 renderer.set_framebuffer(&(target_ms ? target_ms : target[0])->get_framebuffer());
107 for(const Step &s: steps)
109 Renderer::Push _push2(renderer);
111 renderer.set_depth_test(&s.get_depth_test());
112 renderer.set_stencil_test(&s.get_stencil_test());
113 renderer.set_blend(&s.get_blend());
115 if (const Lighting *lighting = s.get_lighting())
116 renderer.add_shader_data(lighting->get_shader_data());
117 renderer.set_clipping(s.get_clipping());
119 if(const Renderable *renderable = s.get_renderable())
120 renderer.render(*renderable, s.get_tag());
126 renderer.resolve_multisample(target[0]->get_framebuffer(), COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
128 renderer.set_depth_test(0);
129 renderer.set_stencil_test(0);
130 renderer.set_blend(0);
132 for(unsigned i=0; i<postproc.size(); ++i)
135 renderer.set_framebuffer(i+1<postproc.size() ? &target[1-j]->get_framebuffer() : out_fbo);
136 const Texture2D &color = target[j]->get_target_texture(COLOR_ATTACHMENT);
137 const Texture2D &depth = target[j]->get_target_texture(DEPTH_ATTACHMENT);
138 postproc[i].postproc->render(renderer, color, depth);
143 void Sequence::set_debug_name(const string &name)
146 for(unsigned i=0; i<2; ++i)
148 target[i]->set_debug_name(format("%s [RT:%d]", name, i));
150 target_ms->set_debug_name(name+" [RT:ms]");
157 Sequence::Step::Step(Tag t, Renderable *r):
164 void Sequence::Step::set_lighting(const Lighting *l)
169 void Sequence::Step::set_depth_test(const DepthTest &dt)
174 void Sequence::Step::set_stencil_test(const StencilTest &st)
179 void Sequence::Step::set_blend(const Blend &b)
184 void Sequence::Step::set_clipping(const Clipping *c)