1 #include <msp/core/maputils.h>
4 #include "framebuffer.h"
6 #include "postprocessor.h"
17 Sequence::Sequence(unsigned w, unsigned h, bool d)
23 Sequence::Sequence(const View &view)
25 init(view.get_width(), view.get_height());
28 Sequence::Sequence(const Framebuffer &fbo)
30 init(fbo.get_width(), fbo.get_height());
33 void Sequence::init(unsigned w, unsigned h)
48 for(vector<PostProcStep>::iterator i=postproc.begin(); i!=postproc.end(); ++i)
56 void Sequence::set_hdr(bool h)
74 void Sequence::set_alpha(bool a)
79 bool old_alpha = alpha;
92 void Sequence::set_multisample(unsigned s)
97 unsigned old_samples = samples;
105 samples = old_samples;
110 Sequence::Step &Sequence::add_step(Tag tag, Renderable &r)
112 steps.push_back(Step(tag, &r));
116 void Sequence::add_postprocessor(PostProcessor &pp)
118 add_postprocessor(&pp, false);
121 void Sequence::add_postprocessor_owned(PostProcessor *pp)
123 add_postprocessor(pp, true);
126 void Sequence::add_postprocessor(PostProcessor *pp, bool owned)
128 postproc.push_back(PostProcStep(pp, owned));
142 void Sequence::setup_frame(Renderer &renderer)
144 for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
145 if(Renderable *renderable = i->get_renderable())
146 renderable->setup_frame(renderer);
149 void Sequence::finish_frame()
151 for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
152 if(Renderable *renderable = i->get_renderable())
153 renderable->finish_frame();
156 void Sequence::render(Renderer &renderer, Tag tag) const
161 Renderer::Push _push(renderer);
163 const Framebuffer *out_fbo = renderer.get_framebuffer();
167 renderer.set_framebuffer(&(samples ? target_ms : target[0])->get_framebuffer());
171 for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
173 Renderer::Push _push2(renderer);
175 renderer.set_depth_test(&i->get_depth_test());
176 renderer.set_stencil_test(&i->get_stencil_test());
177 renderer.set_blend(&i->get_blend());
179 if (const Lighting *lighting = i->get_lighting())
180 renderer.add_shader_data(lighting->get_shader_data());
181 renderer.set_clipping(i->get_clipping());
183 if(const Renderable *renderable = i->get_renderable())
184 renderer.render(*renderable, i->get_tag());
190 renderer.resolve_multisample(target[0]->get_framebuffer(), COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
192 renderer.set_depth_test(0);
193 renderer.set_stencil_test(0);
194 renderer.set_blend(0);
196 for(unsigned i=0; i<postproc.size(); ++i)
199 renderer.set_framebuffer(i+1<postproc.size() ? &target[1-j]->get_framebuffer() : out_fbo);
200 const Texture2D &color = target[j]->get_target_texture(COLOR_ATTACHMENT);
201 const Texture2D &depth = target[j]->get_target_texture(DEPTH_ATTACHMENT);
202 postproc[i].postproc->render(renderer, color, depth);
207 void Sequence::create_targets(unsigned recreate)
222 PixelFormat color_pf = (hdr ? (alpha ? RGBA16F : RGB16F) : (alpha ? RGBA8 : RGB8));
223 FrameFormat fmt = (COLOR_ATTACHMENT,color_pf, DEPTH_ATTACHMENT);
224 if(!postproc.empty() || samples)
227 target[0] = new RenderTarget(width, height, fmt);
228 if(!target[1] && postproc.size()>1)
229 target[1] = new RenderTarget(width, height, fmt);
232 if(!target_ms && samples)
233 target_ms = new RenderTarget(width, height, fmt.set_samples(samples));
236 if(!debug_name.empty())
237 set_target_debug_names();
241 void Sequence::set_debug_name(const string &name)
246 set_target_debug_names();
252 void Sequence::set_target_debug_names()
255 for(unsigned i=0; i<2; ++i)
257 target[i]->set_debug_name(format("%s [RT:%d]", debug_name, i));
259 target_ms->set_debug_name(debug_name+" [RT:ms]");
264 Sequence::Step::Step(Tag t, Renderable *r):
271 void Sequence::Step::set_lighting(const Lighting *l)
276 void Sequence::Step::set_depth_test(const DepthTest &dt)
281 void Sequence::Step::set_stencil_test(const StencilTest &st)
286 void Sequence::Step::set_blend(const Blend &b)
291 void Sequence::Step::set_clipping(const Clipping *c)