1 #include <msp/core/maputils.h>
4 #include "framebuffer.h"
6 #include "postprocessor.h"
7 #include "renderbuffer.h"
10 #include "texture2d.h"
18 Sequence::Sequence(unsigned w, unsigned h, bool d)
24 Sequence::Sequence(const View &view)
26 init(view.get_width(), view.get_height());
29 Sequence::Sequence(const Framebuffer &fbo)
31 init(fbo.get_width(), fbo.get_height());
34 void Sequence::init(unsigned w, unsigned h)
49 for(vector<PostProcStep>::iterator i=postproc.begin(); i!=postproc.end(); ++i)
57 void Sequence::set_hdr(bool h)
75 void Sequence::set_alpha(bool a)
80 bool old_alpha = alpha;
93 void Sequence::set_multisample(unsigned s)
98 unsigned old_samples = samples;
106 samples = old_samples;
111 Sequence::Step &Sequence::add_step(Tag tag, Renderable &r)
113 steps.push_back(Step(tag, &r));
117 void Sequence::add_postprocessor(PostProcessor &pp)
119 add_postprocessor(&pp, false);
122 void Sequence::add_postprocessor_owned(PostProcessor *pp)
124 add_postprocessor(pp, true);
127 void Sequence::add_postprocessor(PostProcessor *pp, bool owned)
129 postproc.push_back(PostProcStep(pp, owned));
143 void Sequence::setup_frame(Renderer &renderer)
145 for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
146 if(Renderable *renderable = i->get_renderable())
147 renderable->setup_frame(renderer);
150 void Sequence::finish_frame()
152 for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
153 if(Renderable *renderable = i->get_renderable())
154 renderable->finish_frame();
157 void Sequence::render(Renderer &renderer, Tag tag) const
162 Renderer::Push _push(renderer);
164 const Framebuffer *out_fbo = renderer.get_framebuffer();
168 renderer.set_framebuffer(&(samples ? target_ms : target[0])->get_framebuffer());
172 for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
174 Renderer::Push _push2(renderer);
176 renderer.set_depth_test(&i->get_depth_test());
177 renderer.set_stencil_test(&i->get_stencil_test());
178 renderer.set_blend(&i->get_blend());
180 if (const Lighting *lighting = i->get_lighting())
181 renderer.add_shader_data(lighting->get_shader_data());
182 renderer.set_clipping(i->get_clipping());
184 if(const Renderable *renderable = i->get_renderable())
185 renderer.render(*renderable, i->get_tag());
191 renderer.resolve_multisample(target[0]->get_framebuffer(), COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
193 renderer.set_depth_test(0);
194 renderer.set_stencil_test(0);
195 renderer.set_blend(0);
197 for(unsigned i=0; i<postproc.size(); ++i)
200 renderer.set_framebuffer(i+1<postproc.size() ? &target[1-j]->get_framebuffer() : out_fbo);
201 const Texture2D &color = target[j]->get_target_texture(RENDER_COLOR);
202 const Texture2D &depth = target[j]->get_target_texture(RENDER_DEPTH);
203 postproc[i].postproc->render(renderer, color, depth);
208 void Sequence::create_targets(unsigned recreate)
223 PixelFormat color_pf = (hdr ? (alpha ? RGBA16F : RGB16F) : (alpha ? RGBA8 : RGB8));
224 RenderTargetFormat fmt = (RENDER_COLOR,color_pf, RENDER_DEPTH);
225 if(!postproc.empty() || samples)
228 target[0] = new RenderTarget(width, height, fmt);
229 if(!target[1] && postproc.size()>1)
230 target[1] = new RenderTarget(width, height, fmt);
233 if(!target_ms && samples)
234 target_ms = new RenderTarget(width, height, samples, fmt);
237 if(!debug_name.empty())
238 set_target_debug_names();
242 void Sequence::set_debug_name(const string &name)
247 set_target_debug_names();
253 void Sequence::set_target_debug_names()
256 for(unsigned i=0; i<2; ++i)
258 target[i]->set_debug_name(format("%s [RT:%d]", debug_name, i));
260 target_ms->set_debug_name(debug_name+" [RT:ms]");
265 Sequence::Step::Step(Tag t, Renderable *r):
272 void Sequence::Step::set_lighting(const Lighting *l)
277 void Sequence::Step::set_depth_test(const DepthTest &dt)
282 void Sequence::Step::set_stencil_test(const StencilTest &st)
287 void Sequence::Step::set_blend(const Blend &b)
292 void Sequence::Step::set_clipping(const Clipping *c)