1 #include <msp/core/maputils.h>
4 #include "framebuffer.h"
6 #include "postprocessor.h"
26 Sequence::Sequence(unsigned w, unsigned h, const FrameFormat &f):
35 if(target_format.empty())
36 throw invalid_argument("Sequence::Sequence");
38 FrameFormat postproc_fmt = target_format;
39 postproc_fmt.set_samples(1);
40 target[0] = new RenderTarget(width, height, postproc_fmt);
41 target[1] = new RenderTarget(width, height, postproc_fmt);
43 if(target_format.get_samples()>1)
44 target_ms = new RenderTarget(width, height, target_format);
49 for(PostProcStep &p: postproc)
57 void Sequence::set_clear_enabled(bool c)
62 void Sequence::set_clear_colors(const vector<Color> &c)
68 void Sequence::set_clear_depth(float d)
74 void Sequence::set_clear_stencil(int s)
80 Sequence::Step &Sequence::add_step(Tag tag, Renderable &r)
82 steps.push_back(Step(tag, &r));
86 void Sequence::add_postprocessor(PostProcessor &pp)
88 add_postprocessor(&pp, false);
91 void Sequence::add_postprocessor_owned(PostProcessor *pp)
93 add_postprocessor(pp, true);
96 void Sequence::add_postprocessor(PostProcessor *pp, bool owned)
98 if(target_format.empty())
102 throw invalid_operation("Sequence::add_postprocessor");
104 postproc.push_back(PostProcStep(pp, owned));
107 void Sequence::setup_frame(Renderer &renderer)
109 for(const Step &s: steps)
110 if(Renderable *renderable = s.get_renderable())
111 renderable->setup_frame(renderer);
114 void Sequence::finish_frame()
116 for(const Step &s: steps)
117 if(Renderable *renderable = s.get_renderable())
118 renderable->finish_frame();
121 void Sequence::render(Renderer &renderer, Tag tag) const
126 Renderer::Push _push(renderer);
128 const Framebuffer *out_fbo = renderer.get_framebuffer();
131 renderer.set_framebuffer(&(target_ms ? target_ms : target[0])->get_framebuffer());
135 const Framebuffer *target_fbo = renderer.get_framebuffer();
137 throw invalid_operation("Sequence::render");
139 const FrameFormat &format = target_fbo->get_format();
140 ClearValue clear_values[7];
142 Color default_color = (clear_colors.empty() ? Color(0.0f, 0.0f, 0.0f, 0.0f) : clear_colors.front());
143 ClearValue *cv = clear_values;
144 for(FrameAttachment a: format)
146 if(get_attach_point(a)==get_attach_point(DEPTH_ATTACHMENT))
147 cv->depth_stencil.depth = clear_depth;
148 else if(get_attach_point(a)==get_attach_point(STENCIL_ATTACHMENT))
149 cv->depth_stencil.stencil = clear_stencil;
151 cv->color = (i<clear_colors.size() ? clear_colors[i++] : default_color);
155 renderer.clear(clear_values);
158 for(const Step &s: steps)
160 Renderer::Push _push2(renderer);
162 renderer.set_depth_test(&s.get_depth_test());
163 renderer.set_stencil_test(&s.get_stencil_test());
164 renderer.set_blend(&s.get_blend());
166 if (const Lighting *lighting = s.get_lighting())
167 renderer.add_shader_data(lighting->get_shader_data());
168 renderer.set_clipping(s.get_clipping());
170 if(const Renderable *renderable = s.get_renderable())
171 renderer.render(*renderable, s.get_tag());
177 renderer.resolve_multisample(target[0]->get_framebuffer());
179 renderer.set_depth_test(0);
180 renderer.set_stencil_test(0);
181 renderer.set_blend(0);
183 for(unsigned i=0; i<postproc.size(); ++i)
186 renderer.set_framebuffer(i+1<postproc.size() ? &target[1-j]->get_framebuffer() : out_fbo);
187 const Texture2D &color = target[j]->get_target_texture(COLOR_ATTACHMENT);
188 const Texture2D &depth = target[j]->get_target_texture(DEPTH_ATTACHMENT);
189 postproc[i].postproc->render(renderer, color, depth);
194 void Sequence::set_debug_name(const string &name)
197 for(unsigned i=0; i<2; ++i)
199 target[i]->set_debug_name(format("%s [RT:%d]", name, i));
201 target_ms->set_debug_name(name+" [RT:ms]");
208 Sequence::Step::Step(Tag t, Renderable *r):
215 void Sequence::Step::set_lighting(const Lighting *l)
220 void Sequence::Step::set_depth_test(const DepthTest &dt)
225 void Sequence::Step::set_stencil_test(const StencilTest &st)
230 void Sequence::Step::set_blend(const Blend &b)
235 void Sequence::Step::set_clipping(const Clipping *c)