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);
169 Framebuffer &fbo = (samples ? target_ms : target[0])->get_framebuffer();
174 for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
176 Renderer::Push push(renderer);
178 renderer.set_depth_test(&i->get_depth_test());
179 renderer.set_stencil_test(&i->get_stencil_test());
180 renderer.set_blend(&i->get_blend());
182 if (const Lighting *lighting = i->get_lighting())
183 renderer.add_shader_data(lighting->get_shader_data());
184 renderer.set_clipping(i->get_clipping());
186 if(const Renderable *renderable = i->get_renderable())
187 renderer.render(*renderable, i->get_tag());
192 renderer.set_depth_test(0);
193 renderer.set_stencil_test(0);
194 renderer.set_blend(0);
197 target[0]->blit_from(*target_ms);
199 for(unsigned i=0; i<postproc.size(); ++i)
202 if(i+1<postproc.size())
203 target[1-j]->get_framebuffer().bind();
206 const Texture2D &color = target[j]->get_target_texture(RENDER_COLOR);
207 const Texture2D &depth = target[j]->get_target_texture(RENDER_DEPTH);
208 postproc[i].postproc->render(renderer, color, depth);
213 void Sequence::create_targets(unsigned recreate)
228 PixelFormat color_pf = (hdr ? (alpha ? RGBA16F : RGB16F) : (alpha ? RGBA8 : RGB8));
229 RenderTargetFormat fmt = (RENDER_COLOR,color_pf, RENDER_DEPTH);
230 if(!postproc.empty() || samples)
233 target[0] = new RenderTarget(width, height, fmt);
234 if(!target[1] && postproc.size()>1)
235 target[1] = new RenderTarget(width, height, fmt);
238 if(!target_ms && samples)
239 target_ms = new RenderTarget(width, height, samples, fmt);
242 if(!debug_name.empty())
243 set_target_debug_names();
247 void Sequence::set_debug_name(const string &name)
252 set_target_debug_names();
258 void Sequence::set_target_debug_names()
261 for(unsigned i=0; i<2; ++i)
263 target[i]->set_debug_name(format("%s [RT:%d]", debug_name, i));
265 target_ms->set_debug_name(debug_name+" [RT:ms]");
270 Sequence::Step::Step(Tag t, Renderable *r):
277 void Sequence::Step::set_lighting(const Lighting *l)
282 void Sequence::Step::set_depth_test(const DepthTest &dt)
287 void Sequence::Step::set_stencil_test(const StencilTest &st)
292 void Sequence::Step::set_blend(const Blend &b)
297 void Sequence::Step::set_clipping(const Clipping *c)