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):
37 void Pipeline::set_hdr(bool h)
43 void Pipeline::set_multisample(unsigned s)
46 create_targets(false);
49 void Pipeline::set_camera(const Camera *c)
54 Pipeline::Pass &Pipeline::add_pass(const Tag &tag)
56 passes.push_back(Pass(tag));
60 void Pipeline::add_renderable(const Renderable &r)
62 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
69 renderables.push_back(&r);
72 void Pipeline::add_renderable_for_pass(const Renderable &r, const Tag &tag)
74 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
77 i->passes.insert(tag);
81 renderables.push_back(&r);
82 renderables.back().passes.insert(tag);
85 void Pipeline::remove_renderable(const Renderable &r)
87 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
95 void Pipeline::add_postprocessor(PostProcessor &pp)
97 postproc.push_back(&pp);
98 create_targets(false);
101 void Pipeline::render(const Tag &tag) const
106 Renderer renderer(camera);
107 render(renderer, tag);
110 void Pipeline::render(Renderer &renderer, const Tag &tag) const
117 Framebuffer &fbo = (samples ? target_ms->fbo : target[0]->fbo);
118 // XXX exception safety
120 fbo.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
123 for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
125 Bind bind_depth_test(i->get_depth_test());
126 Bind bind_blend(i->get_blend());
127 Bind bind_lighting(i->get_lighting());
129 for(vector<Slot>::const_iterator j=renderables.begin(); j!=renderables.end(); ++j)
130 if(j->passes.empty() || j->passes.count(i->get_tag()))
131 renderer.render(*j->renderable, i->get_tag());
137 target[0]->fbo.blit_from(target_ms->fbo, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false);
139 for(unsigned i=0; i<postproc.size(); ++i)
142 if(i+1<postproc.size())
143 target[1-j]->fbo.bind();
145 Framebuffer::unbind();
146 postproc[i]->render(target[j]->color, target[j]->depth);
149 Framebuffer::unbind();
153 void Pipeline::create_targets(bool recreate)
165 PixelFormat fmt = (hdr ? RGB16F : RGB);
167 target[0] = new RenderTarget(width, height, fmt);
168 if(!target[1] && postproc.size()>1)
169 target[1] = new RenderTarget(width, height, fmt);
171 if(!target_ms && samples)
172 target_ms = new MultisampleTarget(width, height, samples, fmt);
176 Pipeline::Pass::Pass(const Tag &t):
183 void Pipeline::Pass::set_lighting(const Lighting *l)
188 void Pipeline::Pass::set_depth_test(const DepthTest *d)
193 void Pipeline::Pass::set_blend(const Blend *b)
199 Pipeline::Slot::Slot(const Renderable *r):
204 Pipeline::RenderTarget::RenderTarget(unsigned w, unsigned h, PixelFormat f)
206 color.set_min_filter(NEAREST);
207 color.set_mag_filter(NEAREST);
208 color.set_wrap(CLAMP_TO_EDGE);
209 color.storage(f, w, h);
210 fbo.attach(COLOR_ATTACHMENT0, color, 0);
212 depth.set_min_filter(NEAREST);
213 depth.set_mag_filter(NEAREST);
214 depth.set_wrap(CLAMP_TO_EDGE);
215 depth.storage(DEPTH_COMPONENT, w, h);
216 fbo.attach(DEPTH_ATTACHMENT, depth, 0);
220 Pipeline::MultisampleTarget::MultisampleTarget(unsigned w, unsigned h, unsigned s, PixelFormat f)
222 color.storage_multisample(s, f, w, h);
223 fbo.attach(COLOR_ATTACHMENT0, color);
225 depth.storage_multisample(s, DEPTH_COMPONENT, w, h);
226 fbo.attach(DEPTH_ATTACHMENT, depth);