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)
55 void Sequence::set_hdr(bool h)
73 void Sequence::set_alpha(bool a)
78 bool old_alpha = alpha;
91 void Sequence::set_multisample(unsigned s)
96 unsigned old_samples = samples;
104 samples = old_samples;
109 Sequence::Step &Sequence::add_step(Tag tag, Renderable &r)
111 steps.push_back(Step(tag, &r));
115 void Sequence::add_postprocessor(PostProcessor &pp)
117 add_postprocessor(&pp, true);
120 void Sequence::add_postprocessor_owned(PostProcessor *pp)
122 add_postprocessor(pp, false);
125 void Sequence::add_postprocessor(PostProcessor *pp, bool keep)
127 postproc.push_back(pp);
129 postproc.back().keep();
141 void Sequence::setup_frame(Renderer &renderer)
143 for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
144 if(Renderable *renderable = i->get_renderable())
145 renderable->setup_frame(renderer);
148 void Sequence::finish_frame()
150 for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
151 if(Renderable *renderable = i->get_renderable())
152 renderable->finish_frame();
155 void Sequence::render(Renderer &renderer, Tag tag) const
160 const Framebuffer *out_fbo = Framebuffer::current();
161 // These are no-ops but will ensure the related state gets restored
162 BindRestore restore_fbo(out_fbo);
163 BindRestore restore_depth_test(DepthTest::current());
164 BindRestore restore_blend(Blend::current());
168 Framebuffer &fbo = (samples ? target_ms : target[0])->get_framebuffer();
173 for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
175 if(const DepthTest *dt = i->get_depth_test())
180 if(const Blend *b = i->get_blend())
185 renderer.set_lighting(i->get_lighting());
186 renderer.set_clipping(i->get_clipping());
188 if(const Renderable *renderable = i->get_renderable())
189 renderer.render(*renderable, i->get_tag());
198 target[0]->blit_from(*target_ms);
200 for(unsigned i=0; i<postproc.size(); ++i)
203 if(i+1<postproc.size())
204 target[1-j]->get_framebuffer().bind();
207 const Texture2D &color = target[j]->get_target_texture(RENDER_COLOR);
208 const Texture2D &depth = target[j]->get_target_texture(RENDER_DEPTH);
209 postproc[i]->render(renderer, color, depth);
214 void Sequence::create_targets(unsigned recreate)
229 PixelFormat color_pf = (hdr ? (alpha ? RGBA16F : RGB16F) : (alpha ? RGBA8 : RGB8));
230 RenderTargetFormat fmt = (RENDER_COLOR,color_pf, RENDER_DEPTH);
231 if(!postproc.empty() || samples)
234 target[0] = new RenderTarget(width, height, fmt);
235 if(!target[1] && postproc.size()>1)
236 target[1] = new RenderTarget(width, height, fmt);
239 if(!target_ms && samples)
240 target_ms = new RenderTarget(width, height, samples, fmt);
244 Sequence::Step::Step(Tag t, Renderable *r):
253 void Sequence::Step::set_lighting(const Lighting *l)
258 void Sequence::Step::set_depth_test(const DepthTest *d)
263 void Sequence::Step::set_blend(const Blend *b)
268 void Sequence::Step::set_clipping(const Clipping *c)