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));
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 void Pipeline::add_postprocessor(PostProcessor &pp)
122 postproc.push_back(&pp);
134 void Pipeline::setup_frame() const
137 for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
138 i->renderable->setup_frame();
141 void Pipeline::finish_frame() const
144 for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
145 i->renderable->finish_frame();
148 void Pipeline::render(const Tag &tag) const
153 Renderer renderer(camera);
154 render(renderer, tag);
157 void Pipeline::render(Renderer &renderer, const Tag &tag) const
162 bool was_in_frame = in_frame;
166 const Framebuffer *out_fbo = Framebuffer::current();
167 // XXX Make sure the correct FBO is restored if an exception is thrown
171 Framebuffer &fbo = (samples ? target_ms->fbo : target[0]->fbo);
173 fbo.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
176 for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
178 Bind bind_depth_test(i->get_depth_test());
179 Bind bind_blend(i->get_blend());
180 renderer.set_lighting(i->get_lighting());
182 for(vector<Slot>::const_iterator j=renderables.begin(); j!=renderables.end(); ++j)
183 if(j->passes.empty() || j->passes.count(i->get_tag()))
184 renderer.render(*j->renderable, i->get_tag());
192 target[0]->fbo.blit_from(target_ms->fbo, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false);
194 for(unsigned i=0; i<postproc.size(); ++i)
197 if(i+1<postproc.size())
198 target[1-j]->fbo.bind();
201 postproc[i]->render(target[j]->color, target[j]->depth);
211 void Pipeline::create_targets(unsigned recreate)
226 PixelFormat fmt = (hdr ? RGB16F : RGB);
227 if(!postproc.empty() || samples)
230 target[0] = new RenderTarget(width, height, fmt);
231 if(!target[1] && postproc.size()>1)
232 target[1] = new RenderTarget(width, height, fmt);
235 if(!target_ms && samples)
236 target_ms = new MultisampleTarget(width, height, samples, fmt);
240 Pipeline::Pass::Pass(const Tag &t):
247 void Pipeline::Pass::set_lighting(const Lighting *l)
252 void Pipeline::Pass::set_depth_test(const DepthTest *d)
257 void Pipeline::Pass::set_blend(const Blend *b)
263 Pipeline::Slot::Slot(const Renderable *r):
268 Pipeline::RenderTarget::RenderTarget(unsigned w, unsigned h, PixelFormat f)
270 color.set_min_filter(NEAREST);
271 color.set_mag_filter(NEAREST);
272 color.set_wrap(CLAMP_TO_EDGE);
273 color.storage(f, w, h);
274 fbo.attach(COLOR_ATTACHMENT0, color, 0);
276 depth.set_min_filter(NEAREST);
277 depth.set_mag_filter(NEAREST);
278 depth.set_wrap(CLAMP_TO_EDGE);
279 depth.storage(DEPTH_COMPONENT, w, h);
280 fbo.attach(DEPTH_ATTACHMENT, depth, 0);
282 fbo.require_complete();
286 Pipeline::MultisampleTarget::MultisampleTarget(unsigned w, unsigned h, unsigned s, PixelFormat f)
288 color.storage_multisample(s, f, w, h);
289 fbo.attach(COLOR_ATTACHMENT0, color);
291 depth.storage_multisample(s, DEPTH_COMPONENT, w, h);
292 fbo.attach(DEPTH_ATTACHMENT, depth);
294 fbo.require_complete();