1 #include <msp/core/maputils.h>
4 #include "framebuffer.h"
7 #include "postprocessor.h"
8 #include "renderbuffer.h"
11 #include "texture2d.h"
18 Pipeline::Pipeline(unsigned w, unsigned h, bool d):
38 void Pipeline::set_hdr(bool h)
56 void Pipeline::set_multisample(unsigned s)
61 unsigned old_samples = samples;
69 samples = old_samples;
74 void Pipeline::set_camera(const Camera *c)
79 Pipeline::Pass &Pipeline::add_pass(const Tag &tag)
81 passes.push_back(Pass(tag, 0));
85 void Pipeline::add_renderable(const Renderable &r)
87 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
94 renderables.push_back(&r);
97 void Pipeline::add_renderable_for_pass(const Renderable &r, const Tag &tag)
99 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
100 if(i->renderable==&r)
102 i->passes.insert(tag);
106 renderables.push_back(&r);
107 renderables.back().passes.insert(tag);
110 void Pipeline::remove_renderable(const Renderable &r)
112 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
113 if(i->renderable==&r)
115 renderables.erase(i);
120 Pipeline::Pass &Pipeline::add_pass(const Tag &tag, const Renderable &r)
122 passes.push_back(Pass(tag, &r));
123 return passes.back();
126 void Pipeline::add_postprocessor(PostProcessor &pp)
128 postproc.push_back(&pp);
140 void Pipeline::setup_frame() const
143 for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
144 if(const Renderable *renderable = i->get_renderable())
145 renderable->setup_frame();
146 for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
147 i->renderable->setup_frame();
150 void Pipeline::finish_frame() const
153 for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
154 if(const Renderable *renderable = i->get_renderable())
155 renderable->finish_frame();
156 for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
157 i->renderable->finish_frame();
160 void Pipeline::render(const Tag &tag) const
165 Renderer renderer(camera);
166 render(renderer, tag);
169 void Pipeline::render(Renderer &renderer, const Tag &tag) const
174 bool was_in_frame = in_frame;
178 const Framebuffer *out_fbo = Framebuffer::current();
179 // XXX Make sure the correct FBO is restored if an exception is thrown
183 Framebuffer &fbo = (samples ? target_ms->fbo : target[0]->fbo);
185 fbo.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
188 for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
190 Bind bind_depth_test(i->get_depth_test());
191 Bind bind_blend(i->get_blend());
192 renderer.set_lighting(i->get_lighting());
193 renderer.set_clipping(i->get_clipping());
195 if(const Renderable *renderable = i->get_renderable())
196 renderer.render(*renderable, i->get_tag());
198 for(vector<Slot>::const_iterator j=renderables.begin(); j!=renderables.end(); ++j)
199 if(j->passes.empty() || j->passes.count(i->get_tag()))
200 renderer.render(*j->renderable, i->get_tag());
208 target[0]->fbo.blit_from(target_ms->fbo, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false);
210 for(unsigned i=0; i<postproc.size(); ++i)
213 if(i+1<postproc.size())
214 target[1-j]->fbo.bind();
217 postproc[i]->render(target[j]->color, target[j]->depth);
227 void Pipeline::create_targets(unsigned recreate)
242 PixelFormat fmt = (hdr ? RGB16F : RGB);
243 if(!postproc.empty() || samples)
246 target[0] = new RenderTarget(width, height, fmt);
247 if(!target[1] && postproc.size()>1)
248 target[1] = new RenderTarget(width, height, fmt);
251 if(!target_ms && samples)
252 target_ms = new MultisampleTarget(width, height, samples, fmt);
256 Pipeline::Pass::Pass(const Tag &t, const Renderable *r):
265 void Pipeline::Pass::set_lighting(const Lighting *l)
270 void Pipeline::Pass::set_depth_test(const DepthTest *d)
275 void Pipeline::Pass::set_blend(const Blend *b)
280 void Pipeline::Pass::set_clipping(const Clipping *c)
286 Pipeline::Slot::Slot(const Renderable *r):
291 Pipeline::RenderTarget::RenderTarget(unsigned w, unsigned h, PixelFormat f)
293 color.set_min_filter(NEAREST);
294 color.set_mag_filter(NEAREST);
295 color.set_wrap(CLAMP_TO_EDGE);
296 color.storage(f, w, h);
297 fbo.attach(COLOR_ATTACHMENT0, color, 0);
299 depth.set_min_filter(NEAREST);
300 depth.set_mag_filter(NEAREST);
301 depth.set_wrap(CLAMP_TO_EDGE);
302 depth.storage(DEPTH_COMPONENT, w, h);
303 fbo.attach(DEPTH_ATTACHMENT, depth, 0);
305 fbo.require_complete();
309 Pipeline::MultisampleTarget::MultisampleTarget(unsigned w, unsigned h, unsigned s, PixelFormat f)
311 color.storage_multisample(s, f, w, h);
312 fbo.attach(COLOR_ATTACHMENT0, color);
314 depth.storage_multisample(s, DEPTH_COMPONENT, w, h);
315 fbo.attach(DEPTH_ATTACHMENT, depth);
317 fbo.require_complete();