1 #include <msp/core/maputils.h>
4 #include "framebuffer.h"
6 #include "postprocessor.h"
24 Sequence::Sequence(unsigned w, unsigned h, const FrameFormat &f):
31 if(target_format.empty())
32 throw invalid_argument("Sequence::Sequence");
34 FrameFormat postproc_fmt = target_format;
35 postproc_fmt.set_samples(1);
36 target[0] = new RenderTarget(width, height, postproc_fmt);
37 target[1] = new RenderTarget(width, height, postproc_fmt);
39 if(target_format.get_samples()>1)
40 target_ms = new RenderTarget(width, height, target_format);
45 for(PostProcStep &p: postproc)
53 void Sequence::set_clear_enabled(bool c)
58 Sequence::Step &Sequence::add_step(Tag tag, Renderable &r)
60 steps.push_back(Step(tag, &r));
64 void Sequence::add_postprocessor(PostProcessor &pp)
66 add_postprocessor(&pp, false);
69 void Sequence::add_postprocessor_owned(PostProcessor *pp)
71 add_postprocessor(pp, true);
74 void Sequence::add_postprocessor(PostProcessor *pp, bool owned)
76 if(target_format.empty())
80 throw invalid_operation("Sequence::add_postprocessor");
82 postproc.push_back(PostProcStep(pp, owned));
85 void Sequence::setup_frame(Renderer &renderer)
87 for(const Step &s: steps)
88 if(Renderable *renderable = s.get_renderable())
89 renderable->setup_frame(renderer);
92 void Sequence::finish_frame()
94 for(const Step &s: steps)
95 if(Renderable *renderable = s.get_renderable())
96 renderable->finish_frame();
99 void Sequence::render(Renderer &renderer, Tag tag) const
104 Renderer::Push _push(renderer);
106 const Framebuffer *out_fbo = renderer.get_framebuffer();
109 renderer.set_framebuffer(&(target_ms ? target_ms : target[0])->get_framebuffer());
114 for(const Step &s: steps)
116 Renderer::Push _push2(renderer);
118 renderer.set_depth_test(&s.get_depth_test());
119 renderer.set_stencil_test(&s.get_stencil_test());
120 renderer.set_blend(&s.get_blend());
122 if (const Lighting *lighting = s.get_lighting())
123 renderer.add_shader_data(lighting->get_shader_data());
124 renderer.set_clipping(s.get_clipping());
126 if(const Renderable *renderable = s.get_renderable())
127 renderer.render(*renderable, s.get_tag());
133 renderer.resolve_multisample(target[0]->get_framebuffer(), COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
135 renderer.set_depth_test(0);
136 renderer.set_stencil_test(0);
137 renderer.set_blend(0);
139 for(unsigned i=0; i<postproc.size(); ++i)
142 renderer.set_framebuffer(i+1<postproc.size() ? &target[1-j]->get_framebuffer() : out_fbo);
143 const Texture2D &color = target[j]->get_target_texture(COLOR_ATTACHMENT);
144 const Texture2D &depth = target[j]->get_target_texture(DEPTH_ATTACHMENT);
145 postproc[i].postproc->render(renderer, color, depth);
150 void Sequence::set_debug_name(const string &name)
153 for(unsigned i=0; i<2; ++i)
155 target[i]->set_debug_name(format("%s [RT:%d]", name, i));
157 target_ms->set_debug_name(name+" [RT:ms]");
164 Sequence::Step::Step(Tag t, Renderable *r):
171 void Sequence::Step::set_lighting(const Lighting *l)
176 void Sequence::Step::set_depth_test(const DepthTest &dt)
181 void Sequence::Step::set_stencil_test(const StencilTest &st)
186 void Sequence::Step::set_blend(const Blend &b)
191 void Sequence::Step::set_clipping(const Clipping *c)