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;
142 Framebuffer &fbo = (samples ? target_ms->fbo : target[0]->fbo);
143 // XXX exception safety
145 fbo.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
148 for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
150 Bind bind_depth_test(i->get_depth_test());
151 Bind bind_blend(i->get_blend());
152 Bind bind_lighting(i->get_lighting());
154 for(vector<Slot>::const_iterator j=renderables.begin(); j!=renderables.end(); ++j)
155 if(j->passes.empty() || j->passes.count(i->get_tag()))
156 renderer.render(*j->renderable, i->get_tag());
162 target[0]->fbo.blit_from(target_ms->fbo, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false);
164 for(unsigned i=0; i<postproc.size(); ++i)
167 if(i+1<postproc.size())
168 target[1-j]->fbo.bind();
170 Framebuffer::unbind();
171 postproc[i]->render(target[j]->color, target[j]->depth);
174 Framebuffer::unbind();
181 void Pipeline::create_targets(unsigned recreate)
196 PixelFormat fmt = (hdr ? RGB16F : RGB);
197 if(!postproc.empty() || samples)
200 target[0] = new RenderTarget(width, height, fmt);
201 if(!target[1] && postproc.size()>1)
202 target[1] = new RenderTarget(width, height, fmt);
205 if(!target_ms && samples)
206 target_ms = new MultisampleTarget(width, height, samples, fmt);
210 Pipeline::Pass::Pass(const Tag &t):
217 void Pipeline::Pass::set_lighting(const Lighting *l)
222 void Pipeline::Pass::set_depth_test(const DepthTest *d)
227 void Pipeline::Pass::set_blend(const Blend *b)
233 Pipeline::Slot::Slot(const Renderable *r):
238 Pipeline::RenderTarget::RenderTarget(unsigned w, unsigned h, PixelFormat f)
240 color.set_min_filter(NEAREST);
241 color.set_mag_filter(NEAREST);
242 color.set_wrap(CLAMP_TO_EDGE);
243 color.storage(f, w, h);
244 fbo.attach(COLOR_ATTACHMENT0, color, 0);
246 depth.set_min_filter(NEAREST);
247 depth.set_mag_filter(NEAREST);
248 depth.set_wrap(CLAMP_TO_EDGE);
249 depth.storage(DEPTH_COMPONENT, w, h);
250 fbo.attach(DEPTH_ATTACHMENT, depth, 0);
252 fbo.require_complete();
256 Pipeline::MultisampleTarget::MultisampleTarget(unsigned w, unsigned h, unsigned s, PixelFormat f)
258 color.storage_multisample(s, f, w, h);
259 fbo.attach(COLOR_ATTACHMENT0, color);
261 depth.storage_multisample(s, DEPTH_COMPONENT, w, h);
262 fbo.attach(DEPTH_ATTACHMENT, depth);
264 fbo.require_complete();