1 #include <msp/core/maputils.h>
3 #include "framebuffer.h"
5 #include "postprocessor.h"
16 Sequence::Sequence(unsigned w, unsigned h, bool d)
22 Sequence::Sequence(const View &view)
24 init(view.get_width(), view.get_height());
27 Sequence::Sequence(const Framebuffer &fbo)
29 init(fbo.get_width(), fbo.get_height());
32 void Sequence::init(unsigned w, unsigned h)
46 for(vector<PostProcStep>::iterator i=postproc.begin(); i!=postproc.end(); ++i)
54 void Sequence::set_hdr(bool h)
72 void Sequence::set_alpha(bool a)
77 bool old_alpha = alpha;
90 void Sequence::set_multisample(unsigned s)
95 unsigned old_samples = samples;
103 samples = old_samples;
108 Sequence::Step &Sequence::add_step(Tag tag, Renderable &r)
110 steps.push_back(Step(tag, &r));
114 void Sequence::add_postprocessor(PostProcessor &pp)
116 add_postprocessor(&pp, false);
119 void Sequence::add_postprocessor_owned(PostProcessor *pp)
121 add_postprocessor(pp, true);
124 void Sequence::add_postprocessor(PostProcessor *pp, bool owned)
126 postproc.push_back(PostProcStep(pp, owned));
140 void Sequence::setup_frame(Renderer &renderer)
142 for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
143 if(Renderable *renderable = i->get_renderable())
144 renderable->setup_frame(renderer);
147 void Sequence::finish_frame()
149 for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
150 if(Renderable *renderable = i->get_renderable())
151 renderable->finish_frame();
154 void Sequence::render(Renderer &renderer, Tag tag) const
159 Renderer::Push _push(renderer);
161 const Framebuffer *out_fbo = renderer.get_framebuffer();
165 renderer.set_framebuffer(&(samples ? target_ms : target[0])->get_framebuffer());
169 for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
171 Renderer::Push _push2(renderer);
173 renderer.set_depth_test(&i->get_depth_test());
174 renderer.set_stencil_test(&i->get_stencil_test());
175 renderer.set_blend(&i->get_blend());
177 if (const Lighting *lighting = i->get_lighting())
178 renderer.add_shader_data(lighting->get_shader_data());
179 renderer.set_clipping(i->get_clipping());
181 if(const Renderable *renderable = i->get_renderable())
182 renderer.render(*renderable, i->get_tag());
188 renderer.resolve_multisample(target[0]->get_framebuffer(), COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
190 renderer.set_depth_test(0);
191 renderer.set_stencil_test(0);
192 renderer.set_blend(0);
194 for(unsigned i=0; i<postproc.size(); ++i)
197 renderer.set_framebuffer(i+1<postproc.size() ? &target[1-j]->get_framebuffer() : out_fbo);
198 const Texture2D &color = target[j]->get_target_texture(COLOR_ATTACHMENT);
199 const Texture2D &depth = target[j]->get_target_texture(DEPTH_ATTACHMENT);
200 postproc[i].postproc->render(renderer, color, depth);
205 void Sequence::create_targets(unsigned recreate)
220 PixelFormat color_pf = (hdr ? (alpha ? RGBA16F : RGB16F) : (alpha ? RGBA8 : RGB8));
221 FrameFormat fmt = (COLOR_ATTACHMENT,color_pf, DEPTH_ATTACHMENT);
222 if(!postproc.empty() || samples)
225 target[0] = new RenderTarget(width, height, fmt);
226 if(!target[1] && postproc.size()>1)
227 target[1] = new RenderTarget(width, height, fmt);
230 if(!target_ms && samples)
231 target_ms = new RenderTarget(width, height, fmt.set_samples(samples));
234 if(!debug_name.empty())
235 set_target_debug_names();
239 void Sequence::set_debug_name(const string &name)
244 set_target_debug_names();
250 void Sequence::set_target_debug_names()
253 for(unsigned i=0; i<2; ++i)
255 target[i]->set_debug_name(format("%s [RT:%d]", debug_name, i));
257 target_ms->set_debug_name(debug_name+" [RT:ms]");
262 Sequence::Step::Step(Tag t, Renderable *r):
269 void Sequence::Step::set_lighting(const Lighting *l)
274 void Sequence::Step::set_depth_test(const DepthTest &dt)
279 void Sequence::Step::set_stencil_test(const StencilTest &st)
284 void Sequence::Step::set_blend(const Blend &b)
289 void Sequence::Step::set_clipping(const Clipping *c)