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)
44 void Pipeline::set_multisample(unsigned s)
47 create_targets(false);
50 void Pipeline::set_camera(const Camera *c)
55 Pipeline::Pass &Pipeline::add_pass(const Tag &tag)
57 passes.push_back(Pass(tag));
61 void Pipeline::add_renderable(const Renderable &r)
63 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
70 renderables.push_back(&r);
73 void Pipeline::add_renderable_for_pass(const Renderable &r, const Tag &tag)
75 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
78 i->passes.insert(tag);
82 renderables.push_back(&r);
83 renderables.back().passes.insert(tag);
86 void Pipeline::remove_renderable(const Renderable &r)
88 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
96 void Pipeline::add_postprocessor(PostProcessor &pp)
98 postproc.push_back(&pp);
99 create_targets(false);
102 void Pipeline::setup_frame() const
105 for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
106 i->renderable->setup_frame();
109 void Pipeline::finish_frame() const
112 for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
113 i->renderable->finish_frame();
116 void Pipeline::render(const Tag &tag) const
121 Renderer renderer(camera);
122 render(renderer, tag);
125 void Pipeline::render(Renderer &renderer, const Tag &tag) const
130 bool was_in_frame = in_frame;
136 Framebuffer &fbo = (samples ? target_ms->fbo : target[0]->fbo);
137 // XXX exception safety
139 fbo.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
142 for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
144 Bind bind_depth_test(i->get_depth_test());
145 Bind bind_blend(i->get_blend());
146 Bind bind_lighting(i->get_lighting());
148 for(vector<Slot>::const_iterator j=renderables.begin(); j!=renderables.end(); ++j)
149 if(j->passes.empty() || j->passes.count(i->get_tag()))
150 renderer.render(*j->renderable, i->get_tag());
156 target[0]->fbo.blit_from(target_ms->fbo, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false);
158 for(unsigned i=0; i<postproc.size(); ++i)
161 if(i+1<postproc.size())
162 target[1-j]->fbo.bind();
164 Framebuffer::unbind();
165 postproc[i]->render(target[j]->color, target[j]->depth);
168 Framebuffer::unbind();
175 void Pipeline::create_targets(bool recreate)
187 PixelFormat fmt = (hdr ? RGB16F : RGB);
189 target[0] = new RenderTarget(width, height, fmt);
190 if(!target[1] && postproc.size()>1)
191 target[1] = new RenderTarget(width, height, fmt);
193 if(!target_ms && samples)
194 target_ms = new MultisampleTarget(width, height, samples, fmt);
198 Pipeline::Pass::Pass(const Tag &t):
205 void Pipeline::Pass::set_lighting(const Lighting *l)
210 void Pipeline::Pass::set_depth_test(const DepthTest *d)
215 void Pipeline::Pass::set_blend(const Blend *b)
221 Pipeline::Slot::Slot(const Renderable *r):
226 Pipeline::RenderTarget::RenderTarget(unsigned w, unsigned h, PixelFormat f)
228 color.set_min_filter(NEAREST);
229 color.set_mag_filter(NEAREST);
230 color.set_wrap(CLAMP_TO_EDGE);
231 color.storage(f, w, h);
232 fbo.attach(COLOR_ATTACHMENT0, color, 0);
234 depth.set_min_filter(NEAREST);
235 depth.set_mag_filter(NEAREST);
236 depth.set_wrap(CLAMP_TO_EDGE);
237 depth.storage(DEPTH_COMPONENT, w, h);
238 fbo.attach(DEPTH_ATTACHMENT, depth, 0);
242 Pipeline::MultisampleTarget::MultisampleTarget(unsigned w, unsigned h, unsigned s, PixelFormat f)
244 color.storage_multisample(s, f, w, h);
245 fbo.attach(COLOR_ATTACHMENT0, color);
247 depth.storage_multisample(s, DEPTH_COMPONENT, w, h);
248 fbo.attach(DEPTH_ATTACHMENT, depth);