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 // These is a no-ops but will ensure the related state gets restored
180 BindRestore restore_fbo(out_fbo);
181 BindRestore restore_depth_test(DepthTest::current());
182 BindRestore restore_blend(Blend::current());
186 Framebuffer &fbo = (samples ? target_ms->fbo : target[0]->fbo);
188 fbo.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
192 for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
194 if(const DepthTest *dt = i->get_depth_test())
199 if(const Blend *b = i->get_blend())
204 renderer.set_lighting(i->get_lighting());
205 renderer.set_clipping(i->get_clipping());
207 if(const Renderable *renderable = i->get_renderable())
208 renderer.render(*renderable, i->get_tag());
210 for(vector<Slot>::const_iterator j=renderables.begin(); j!=renderables.end(); ++j)
211 if(j->passes.empty() || j->passes.count(i->get_tag()))
212 renderer.render(*j->renderable, i->get_tag());
217 BindRestore unbind_depth_test(static_cast<DepthTest *>(0));
218 BindRestore unbind_blend(static_cast<Blend *>(0));
221 target[0]->fbo.blit_from(target_ms->fbo, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false);
223 for(unsigned i=0; i<postproc.size(); ++i)
226 if(i+1<postproc.size())
227 target[1-j]->fbo.bind();
230 postproc[i]->render(renderer, target[j]->color, target[j]->depth);
238 void Pipeline::create_targets(unsigned recreate)
253 PixelFormat fmt = (hdr ? RGB16F : RGB);
254 if(!postproc.empty() || samples)
257 target[0] = new RenderTarget(width, height, fmt);
258 if(!target[1] && postproc.size()>1)
259 target[1] = new RenderTarget(width, height, fmt);
262 if(!target_ms && samples)
263 target_ms = new MultisampleTarget(width, height, samples, fmt);
267 Pipeline::Pass::Pass(const Tag &t, const Renderable *r):
276 void Pipeline::Pass::set_lighting(const Lighting *l)
281 void Pipeline::Pass::set_depth_test(const DepthTest *d)
286 void Pipeline::Pass::set_blend(const Blend *b)
291 void Pipeline::Pass::set_clipping(const Clipping *c)
297 Pipeline::Slot::Slot(const Renderable *r):
302 Pipeline::RenderTarget::RenderTarget(unsigned w, unsigned h, PixelFormat f)
304 color.set_min_filter(NEAREST);
305 color.set_mag_filter(NEAREST);
306 color.set_wrap(CLAMP_TO_EDGE);
307 color.storage(f, w, h);
308 fbo.attach(COLOR_ATTACHMENT0, color, 0);
310 depth.set_min_filter(NEAREST);
311 depth.set_mag_filter(NEAREST);
312 depth.set_wrap(CLAMP_TO_EDGE);
313 depth.storage(DEPTH_COMPONENT, w, h);
314 fbo.attach(DEPTH_ATTACHMENT, depth, 0);
316 fbo.require_complete();
320 Pipeline::MultisampleTarget::MultisampleTarget(unsigned w, unsigned h, unsigned s, PixelFormat f)
322 color.storage_multisample(s, f, w, h);
323 fbo.attach(COLOR_ATTACHMENT0, color);
325 depth.storage_multisample(s, DEPTH_COMPONENT, w, h);
326 fbo.attach(DEPTH_ATTACHMENT, depth);
328 fbo.require_complete();