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 // This is a no-op but will ensure the FBO binding gets restored
180 BindRestore restore_fbo(out_fbo);
184 Framebuffer &fbo = (samples ? target_ms->fbo : target[0]->fbo);
186 fbo.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
189 for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
191 Bind bind_depth_test(i->get_depth_test());
192 Bind bind_blend(i->get_blend());
193 renderer.set_lighting(i->get_lighting());
194 renderer.set_clipping(i->get_clipping());
196 if(const Renderable *renderable = i->get_renderable())
197 renderer.render(*renderable, i->get_tag());
199 for(vector<Slot>::const_iterator j=renderables.begin(); j!=renderables.end(); ++j)
200 if(j->passes.empty() || j->passes.count(i->get_tag()))
201 renderer.render(*j->renderable, i->get_tag());
209 target[0]->fbo.blit_from(target_ms->fbo, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false);
211 for(unsigned i=0; i<postproc.size(); ++i)
214 if(i+1<postproc.size())
215 target[1-j]->fbo.bind();
218 postproc[i]->render(target[j]->color, target[j]->depth);
226 void Pipeline::create_targets(unsigned recreate)
241 PixelFormat fmt = (hdr ? RGB16F : RGB);
242 if(!postproc.empty() || samples)
245 target[0] = new RenderTarget(width, height, fmt);
246 if(!target[1] && postproc.size()>1)
247 target[1] = new RenderTarget(width, height, fmt);
250 if(!target_ms && samples)
251 target_ms = new MultisampleTarget(width, height, samples, fmt);
255 Pipeline::Pass::Pass(const Tag &t, const Renderable *r):
264 void Pipeline::Pass::set_lighting(const Lighting *l)
269 void Pipeline::Pass::set_depth_test(const DepthTest *d)
274 void Pipeline::Pass::set_blend(const Blend *b)
279 void Pipeline::Pass::set_clipping(const Clipping *c)
285 Pipeline::Slot::Slot(const Renderable *r):
290 Pipeline::RenderTarget::RenderTarget(unsigned w, unsigned h, PixelFormat f)
292 color.set_min_filter(NEAREST);
293 color.set_mag_filter(NEAREST);
294 color.set_wrap(CLAMP_TO_EDGE);
295 color.storage(f, w, h);
296 fbo.attach(COLOR_ATTACHMENT0, color, 0);
298 depth.set_min_filter(NEAREST);
299 depth.set_mag_filter(NEAREST);
300 depth.set_wrap(CLAMP_TO_EDGE);
301 depth.storage(DEPTH_COMPONENT, w, h);
302 fbo.attach(DEPTH_ATTACHMENT, depth, 0);
304 fbo.require_complete();
308 Pipeline::MultisampleTarget::MultisampleTarget(unsigned w, unsigned h, unsigned s, PixelFormat f)
310 color.storage_multisample(s, f, w, h);
311 fbo.attach(COLOR_ATTACHMENT0, color);
313 depth.storage_multisample(s, DEPTH_COMPONENT, w, h);
314 fbo.attach(DEPTH_ATTACHMENT, depth);
316 fbo.require_complete();