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):
42 void Pipeline::set_hdr(bool h)
49 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);
111 void Pipeline::render(const Tag &tag) const
116 Renderer renderer(camera);
117 render(renderer, tag);
120 void Pipeline::render(Renderer &renderer, const Tag &tag) const
127 Framebuffer *f = (fbo_ms ? fbo_ms : fbo);
128 // XXX exception safety
130 f->clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
133 for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
135 Bind bind_depth_test(i->get_depth_test());
136 Bind bind_blend(i->get_blend());
137 Bind bind_lighting(i->get_lighting());
139 for(vector<Slot>::const_iterator j=renderables.begin(); j!=renderables.end(); ++j)
140 if(j->passes.empty() || j->passes.count(i->get_tag()))
141 j->renderable->render(renderer, i->get_tag());
147 fbo->blit_from(*fbo_ms, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false);
148 Framebuffer::unbind();
151 // XXX Need two color buffer textures to handle multiple post-processors correctly
152 for(vector<PostProcessor *>::const_iterator i=postproc.begin(); i!=postproc.end(); ++i)
153 (*i)->render(*color_buf, *depth_buf);
156 void Pipeline::create_fbos()
169 fbo = new Framebuffer;
171 color_buf = new Texture2D;
172 color_buf->set_min_filter(NEAREST);
173 color_buf->set_mag_filter(NEAREST);
174 color_buf->set_wrap(CLAMP_TO_EDGE);
175 color_buf->storage((hdr ? RGB16F : RGB), width, height);
176 fbo->attach(COLOR_ATTACHMENT0, *color_buf, 0);
178 depth_buf = new Texture2D;
179 depth_buf->set_min_filter(NEAREST);
180 depth_buf->set_mag_filter(NEAREST);
181 depth_buf->set_wrap(CLAMP_TO_EDGE);
182 depth_buf->storage(DEPTH_COMPONENT, width, height);
183 fbo->attach(DEPTH_ATTACHMENT, *depth_buf, 0);
187 fbo_ms = new Framebuffer;
189 color_buf_ms = new Renderbuffer;
190 color_buf_ms->storage_multisample(samples, (hdr ? RGB16F : RGB), width, height);
191 fbo_ms->attach(COLOR_ATTACHMENT0, *color_buf_ms);
193 depth_buf_ms = new Renderbuffer;
194 depth_buf_ms->storage_multisample(samples, DEPTH_COMPONENT, width, height);
195 fbo_ms->attach(DEPTH_ATTACHMENT, *depth_buf_ms);
200 Pipeline::Pass::Pass(const Tag &t):
207 void Pipeline::Pass::set_lighting(const Lighting *l)
212 void Pipeline::Pass::set_depth_test(const DepthTest *d)
217 void Pipeline::Pass::set_blend(const Blend *b)
223 Pipeline::Slot::Slot(const Renderable *r):