1 #include <msp/core/maputils.h>
4 #include "framebuffer.h"
6 #include "postprocessor.h"
7 #include "renderbuffer.h"
11 #include "texture2d.h"
19 Sequence::Sequence(unsigned w, unsigned h, bool d)
25 Sequence::Sequence(const View &view)
27 init(view.get_width(), view.get_height());
30 Sequence::Sequence(const Framebuffer &fbo)
32 init(fbo.get_width(), fbo.get_height());
35 void Sequence::init(unsigned w, unsigned h)
50 for(vector<PostProcStep>::iterator i=postproc.begin(); i!=postproc.end(); ++i)
58 void Sequence::set_hdr(bool h)
76 void Sequence::set_alpha(bool a)
81 bool old_alpha = alpha;
94 void Sequence::set_multisample(unsigned s)
99 unsigned old_samples = samples;
107 samples = old_samples;
112 Sequence::Step &Sequence::add_step(Tag tag, Renderable &r)
114 steps.push_back(Step(tag, &r));
118 void Sequence::add_postprocessor(PostProcessor &pp)
120 add_postprocessor(&pp, false);
123 void Sequence::add_postprocessor_owned(PostProcessor *pp)
125 add_postprocessor(pp, true);
128 void Sequence::add_postprocessor(PostProcessor *pp, bool owned)
130 postproc.push_back(PostProcStep(pp, owned));
144 void Sequence::setup_frame(Renderer &renderer)
146 for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
147 if(Renderable *renderable = i->get_renderable())
148 renderable->setup_frame(renderer);
151 void Sequence::finish_frame()
153 for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
154 if(Renderable *renderable = i->get_renderable())
155 renderable->finish_frame();
158 void Sequence::render(Renderer &renderer, Tag tag) const
163 const Framebuffer *out_fbo = Framebuffer::current();
164 // These are no-ops but will ensure the related state gets restored
165 BindRestore restore_fbo(out_fbo);
166 BindRestore restore_depth_test(DepthTest::current());
167 BindRestore restore_blend(Blend::current());
171 Framebuffer &fbo = (samples ? target_ms : target[0])->get_framebuffer();
176 for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
178 if(const DepthTest *dt = i->get_depth_test())
183 if(const Blend *b = i->get_blend())
188 if (const Lighting *lighting = i->get_lighting())
189 renderer.add_shader_data(lighting->get_shader_data());
190 renderer.set_clipping(i->get_clipping());
192 if(const Renderable *renderable = i->get_renderable())
193 renderer.render(*renderable, i->get_tag());
202 target[0]->blit_from(*target_ms);
204 for(unsigned i=0; i<postproc.size(); ++i)
207 if(i+1<postproc.size())
208 target[1-j]->get_framebuffer().bind();
211 const Texture2D &color = target[j]->get_target_texture(RENDER_COLOR);
212 const Texture2D &depth = target[j]->get_target_texture(RENDER_DEPTH);
213 postproc[i].postproc->render(renderer, color, depth);
218 void Sequence::create_targets(unsigned recreate)
233 PixelFormat color_pf = (hdr ? (alpha ? RGBA16F : RGB16F) : (alpha ? RGBA8 : RGB8));
234 RenderTargetFormat fmt = (RENDER_COLOR,color_pf, RENDER_DEPTH);
235 if(!postproc.empty() || samples)
238 target[0] = new RenderTarget(width, height, fmt);
239 if(!target[1] && postproc.size()>1)
240 target[1] = new RenderTarget(width, height, fmt);
243 if(!target_ms && samples)
244 target_ms = new RenderTarget(width, height, samples, fmt);
247 if(!debug_name.empty())
248 set_target_debug_names();
252 void Sequence::set_debug_name(const string &name)
257 set_target_debug_names();
263 void Sequence::set_target_debug_names()
266 for(unsigned i=0; i<2; ++i)
268 target[i]->set_debug_name(format("%s [RT:%d]", debug_name, i));
270 target_ms->set_debug_name(debug_name+" [RT:ms]");
275 Sequence::Step::Step(Tag t, Renderable *r):
284 void Sequence::Step::set_lighting(const Lighting *l)
289 void Sequence::Step::set_depth_test(const DepthTest *d)
294 void Sequence::Step::set_blend(const Blend *b)
299 void Sequence::Step::set_clipping(const Clipping *c)