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)
47 void Pipeline::set_multisample(unsigned s)
56 void Pipeline::set_camera(const Camera *c)
61 Pipeline::Pass &Pipeline::add_pass(const Tag &tag)
63 passes.push_back(Pass(tag));
67 void Pipeline::add_renderable(const Renderable &r)
69 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
76 renderables.push_back(&r);
79 void Pipeline::add_renderable_for_pass(const Renderable &r, const Tag &tag)
81 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
84 i->passes.insert(tag);
88 renderables.push_back(&r);
89 renderables.back().passes.insert(tag);
92 void Pipeline::remove_renderable(const Renderable &r)
94 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
102 void Pipeline::add_postprocessor(PostProcessor &pp)
104 postproc.push_back(&pp);
108 void Pipeline::setup_frame() const
111 for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
112 i->renderable->setup_frame();
115 void Pipeline::finish_frame() const
118 for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
119 i->renderable->finish_frame();
122 void Pipeline::render(const Tag &tag) const
127 Renderer renderer(camera);
128 render(renderer, tag);
131 void Pipeline::render(Renderer &renderer, const Tag &tag) const
136 bool was_in_frame = in_frame;
140 const Framebuffer *out_fbo = Framebuffer::current();
141 /* Binding the current object is a no-op, but this will restore the original
142 FBO in case an exception is thrown. */
143 Bind restore_fbo(out_fbo, true);
147 Framebuffer &fbo = (samples ? target_ms->fbo : target[0]->fbo);
149 fbo.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
152 for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
154 Bind bind_depth_test(i->get_depth_test());
155 Bind bind_blend(i->get_blend());
156 renderer.set_lighting(i->get_lighting());
158 for(vector<Slot>::const_iterator j=renderables.begin(); j!=renderables.end(); ++j)
159 if(j->passes.empty() || j->passes.count(i->get_tag()))
160 renderer.render(*j->renderable, i->get_tag());
166 target[0]->fbo.blit_from(target_ms->fbo, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false);
168 for(unsigned i=0; i<postproc.size(); ++i)
171 if(i+1<postproc.size())
172 target[1-j]->fbo.bind();
175 postproc[i]->render(target[j]->color, target[j]->depth);
185 void Pipeline::create_targets(unsigned recreate)
200 PixelFormat fmt = (hdr ? RGB16F : RGB);
201 if(!postproc.empty() || samples)
204 target[0] = new RenderTarget(width, height, fmt);
205 if(!target[1] && postproc.size()>1)
206 target[1] = new RenderTarget(width, height, fmt);
209 if(!target_ms && samples)
210 target_ms = new MultisampleTarget(width, height, samples, fmt);
214 Pipeline::Pass::Pass(const Tag &t):
221 void Pipeline::Pass::set_lighting(const Lighting *l)
226 void Pipeline::Pass::set_depth_test(const DepthTest *d)
231 void Pipeline::Pass::set_blend(const Blend *b)
237 Pipeline::Slot::Slot(const Renderable *r):
242 Pipeline::RenderTarget::RenderTarget(unsigned w, unsigned h, PixelFormat f)
244 color.set_min_filter(NEAREST);
245 color.set_mag_filter(NEAREST);
246 color.set_wrap(CLAMP_TO_EDGE);
247 color.storage(f, w, h);
248 fbo.attach(COLOR_ATTACHMENT0, color, 0);
250 depth.set_min_filter(NEAREST);
251 depth.set_mag_filter(NEAREST);
252 depth.set_wrap(CLAMP_TO_EDGE);
253 depth.storage(DEPTH_COMPONENT, w, h);
254 fbo.attach(DEPTH_ATTACHMENT, depth, 0);
256 fbo.require_complete();
260 Pipeline::MultisampleTarget::MultisampleTarget(unsigned w, unsigned h, unsigned s, PixelFormat f)
262 color.storage_multisample(s, f, w, h);
263 fbo.attach(COLOR_ATTACHMENT0, color);
265 depth.storage_multisample(s, DEPTH_COMPONENT, w, h);
266 fbo.attach(DEPTH_ATTACHMENT, depth);
268 fbo.require_complete();