1 #include <msp/core/maputils.h>
4 #include "framebuffer.h"
7 #include "postprocessor.h"
8 #include "renderbuffer.h"
11 #include "texture2d.h"
19 Pipeline::Pipeline(unsigned w, unsigned h, bool d)
25 Pipeline::Pipeline(const View &view)
27 init(view.get_width(), view.get_height());
30 Pipeline::Pipeline(const Framebuffer &fbo)
32 init(fbo.get_width(), fbo.get_height());
35 void Pipeline::init(unsigned w, unsigned h)
55 void Pipeline::set_hdr(bool h)
73 void Pipeline::set_alpha(bool a)
78 bool old_alpha = alpha;
91 void Pipeline::set_multisample(unsigned s)
96 unsigned old_samples = samples;
104 samples = old_samples;
109 void Pipeline::set_camera(const Camera *c)
114 Pipeline::Pass &Pipeline::add_pass(const Tag &tag)
116 passes.push_back(Pass(tag, 0));
117 return passes.back();
120 void Pipeline::add_renderable(Renderable &r)
122 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
123 if(i->renderable==&r)
129 renderables.push_back(&r);
132 void Pipeline::add_renderable_for_pass(Renderable &r, const Tag &tag)
134 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
135 if(i->renderable==&r)
137 i->passes.insert(tag);
141 renderables.push_back(&r);
142 renderables.back().passes.insert(tag);
145 void Pipeline::remove_renderable(Renderable &r)
147 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
148 if(i->renderable==&r)
150 renderables.erase(i);
155 Pipeline::Pass &Pipeline::add_pass(const Tag &tag, Renderable &r)
157 passes.push_back(Pass(tag, &r));
158 return passes.back();
161 void Pipeline::add_postprocessor(PostProcessor &pp)
163 add_postprocessor(&pp, true);
166 void Pipeline::add_postprocessor_owned(PostProcessor *pp)
168 add_postprocessor(pp, false);
171 void Pipeline::add_postprocessor(PostProcessor *pp, bool keep)
173 postproc.push_back(pp);
175 postproc.back().keep();
187 void Pipeline::setup_frame(Renderer &renderer)
189 for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
190 if(Renderable *renderable = i->get_renderable())
191 renderable->setup_frame(renderer);
192 for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
193 i->renderable->setup_frame(renderer);
196 void Pipeline::finish_frame()
198 for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
199 if(Renderable *renderable = i->get_renderable())
200 renderable->finish_frame();
201 for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
202 i->renderable->finish_frame();
205 void Pipeline::render()
207 Renderer renderer(camera);
208 setup_frame(renderer);
213 void Pipeline::render(Renderer &renderer, const Tag &tag) const
218 const Framebuffer *out_fbo = Framebuffer::current();
219 // These are no-ops but will ensure the related state gets restored
220 BindRestore restore_fbo(out_fbo);
221 BindRestore restore_depth_test(DepthTest::current());
222 BindRestore restore_blend(Blend::current());
226 Framebuffer &fbo = (samples ? target_ms : target[0])->get_framebuffer();
228 fbo.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
231 for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
233 if(const DepthTest *dt = i->get_depth_test())
238 if(const Blend *b = i->get_blend())
243 renderer.set_lighting(i->get_lighting());
244 renderer.set_clipping(i->get_clipping());
246 if(const Renderable *renderable = i->get_renderable())
247 renderer.render(*renderable, i->get_tag());
249 for(vector<Slot>::const_iterator j=renderables.begin(); j!=renderables.end(); ++j)
250 if(j->passes.empty() || j->passes.count(i->get_tag()))
251 renderer.render(*j->renderable, i->get_tag());
260 target[0]->blit_from(*target_ms);
262 for(unsigned i=0; i<postproc.size(); ++i)
265 if(i+1<postproc.size())
266 target[1-j]->get_framebuffer().bind();
269 const Texture2D &color = target[j]->get_target_texture(RENDER_COLOR);
270 const Texture2D &depth = target[j]->get_target_texture(RENDER_DEPTH);
271 postproc[i]->render(renderer, color, depth);
276 void Pipeline::create_targets(unsigned recreate)
291 PixelFormat color_pf = (hdr ? (alpha ? RGBA16F : RGB16F) : (alpha ? RGBA : RGB));
292 RenderTargetFormat fmt = (RENDER_COLOR,color_pf, RENDER_DEPTH);
293 if(!postproc.empty() || samples)
296 target[0] = new RenderTarget(width, height, fmt);
297 if(!target[1] && postproc.size()>1)
298 target[1] = new RenderTarget(width, height, fmt);
301 if(!target_ms && samples)
302 target_ms = new RenderTarget(width, height, samples, fmt);
306 Pipeline::Pass::Pass(const Tag &t, Renderable *r):
315 void Pipeline::Pass::set_lighting(const Lighting *l)
320 void Pipeline::Pass::set_depth_test(const DepthTest *d)
325 void Pipeline::Pass::set_blend(const Blend *b)
330 void Pipeline::Pass::set_clipping(const Clipping *c)
336 Pipeline::Slot::Slot(Renderable *r):