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)
52 unsigned old_samples = samples;
60 samples = old_samples;
65 void Pipeline::set_camera(const Camera *c)
70 Pipeline::Pass &Pipeline::add_pass(const Tag &tag)
72 passes.push_back(Pass(tag));
76 void Pipeline::add_renderable(const Renderable &r)
78 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
85 renderables.push_back(&r);
88 void Pipeline::add_renderable_for_pass(const Renderable &r, const Tag &tag)
90 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
93 i->passes.insert(tag);
97 renderables.push_back(&r);
98 renderables.back().passes.insert(tag);
101 void Pipeline::remove_renderable(const Renderable &r)
103 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
104 if(i->renderable==&r)
106 renderables.erase(i);
111 void Pipeline::add_postprocessor(PostProcessor &pp)
113 postproc.push_back(&pp);
117 void Pipeline::setup_frame() const
120 for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
121 i->renderable->setup_frame();
124 void Pipeline::finish_frame() const
127 for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
128 i->renderable->finish_frame();
131 void Pipeline::render(const Tag &tag) const
136 Renderer renderer(camera);
137 render(renderer, tag);
140 void Pipeline::render(Renderer &renderer, const Tag &tag) const
145 bool was_in_frame = in_frame;
149 const Framebuffer *out_fbo = Framebuffer::current();
150 // XXX Make sure the correct FBO is restored if an exception is thrown
154 Framebuffer &fbo = (samples ? target_ms->fbo : target[0]->fbo);
156 fbo.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
159 for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
161 Bind bind_depth_test(i->get_depth_test());
162 Bind bind_blend(i->get_blend());
163 renderer.set_lighting(i->get_lighting());
165 for(vector<Slot>::const_iterator j=renderables.begin(); j!=renderables.end(); ++j)
166 if(j->passes.empty() || j->passes.count(i->get_tag()))
167 renderer.render(*j->renderable, i->get_tag());
175 target[0]->fbo.blit_from(target_ms->fbo, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false);
177 for(unsigned i=0; i<postproc.size(); ++i)
180 if(i+1<postproc.size())
181 target[1-j]->fbo.bind();
184 postproc[i]->render(target[j]->color, target[j]->depth);
194 void Pipeline::create_targets(unsigned recreate)
209 PixelFormat fmt = (hdr ? RGB16F : RGB);
210 if(!postproc.empty() || samples)
213 target[0] = new RenderTarget(width, height, fmt);
214 if(!target[1] && postproc.size()>1)
215 target[1] = new RenderTarget(width, height, fmt);
218 if(!target_ms && samples)
219 target_ms = new MultisampleTarget(width, height, samples, fmt);
223 Pipeline::Pass::Pass(const Tag &t):
230 void Pipeline::Pass::set_lighting(const Lighting *l)
235 void Pipeline::Pass::set_depth_test(const DepthTest *d)
240 void Pipeline::Pass::set_blend(const Blend *b)
246 Pipeline::Slot::Slot(const Renderable *r):
251 Pipeline::RenderTarget::RenderTarget(unsigned w, unsigned h, PixelFormat f)
253 color.set_min_filter(NEAREST);
254 color.set_mag_filter(NEAREST);
255 color.set_wrap(CLAMP_TO_EDGE);
256 color.storage(f, w, h);
257 fbo.attach(COLOR_ATTACHMENT0, color, 0);
259 depth.set_min_filter(NEAREST);
260 depth.set_mag_filter(NEAREST);
261 depth.set_wrap(CLAMP_TO_EDGE);
262 depth.storage(DEPTH_COMPONENT, w, h);
263 fbo.attach(DEPTH_ATTACHMENT, depth, 0);
265 fbo.require_complete();
269 Pipeline::MultisampleTarget::MultisampleTarget(unsigned w, unsigned h, unsigned s, PixelFormat f)
271 color.storage_multisample(s, f, w, h);
272 fbo.attach(COLOR_ATTACHMENT0, color);
274 depth.storage_multisample(s, DEPTH_COMPONENT, w, h);
275 fbo.attach(DEPTH_ATTACHMENT, depth);
277 fbo.require_complete();