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 Pass &pass = insert_unique(passes, tag, Pass())->second;
64 pass_order.push_back(tag);
68 Pipeline::Pass &Pipeline::get_pass(const Tag &tag)
70 return get_item(passes, tag);
73 const Pipeline::Pass &Pipeline::get_pass(const Tag &tag) const
75 return get_item(passes, tag);
78 void Pipeline::add_renderable(const Renderable &r)
80 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
87 renderables.push_back(&r);
90 void Pipeline::add_renderable_for_pass(const Renderable &r, const Tag &tag)
92 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
95 i->passes.insert(tag);
99 renderables.push_back(&r);
100 renderables.back().passes.insert(tag);
103 void Pipeline::remove_renderable(const Renderable &r)
105 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
106 if(i->renderable==&r)
108 renderables.erase(i);
113 void Pipeline::add_postprocessor(PostProcessor &pp)
115 postproc.push_back(&pp);
122 void Pipeline::render(Renderer &renderer, const Tag &tag) const
124 const Pass &pass = get_pass(tag);
126 Bind bind_depth_test(pass.get_depth_test());
127 Bind bind_blend(pass.get_blend());
128 Bind bind_lighting(pass.get_lighting());
130 for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
131 if(i->passes.empty() || i->passes.count(tag))
132 i->renderable->render(renderer, tag);
135 void Pipeline::render_all() const
142 Framebuffer *f = (fbo_ms ? fbo_ms : fbo);
144 f->clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
147 Renderer renderer(camera);
148 for(vector<Tag>::const_iterator i=pass_order.begin(); i!=pass_order.end(); ++i)
149 render(renderer, *i);
154 fbo->blit_from(*fbo_ms, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false);
155 Framebuffer::unbind();
158 // XXX Need two color buffer textures to handle multiple post-processors correctly
159 for(vector<PostProcessor *>::const_iterator i=postproc.begin(); i!=postproc.end(); ++i)
160 (*i)->render(*color_buf, *depth_buf);
163 void Pipeline::create_fbos()
176 fbo = new Framebuffer;
178 color_buf = new Texture2D;
179 color_buf->set_min_filter(NEAREST);
180 color_buf->set_mag_filter(NEAREST);
181 color_buf->set_wrap(CLAMP_TO_EDGE);
182 color_buf->storage((hdr ? RGB16F : RGB), width, height);
183 fbo->attach(COLOR_ATTACHMENT0, *color_buf, 0);
185 depth_buf = new Texture2D;
186 depth_buf->set_min_filter(NEAREST);
187 depth_buf->set_mag_filter(NEAREST);
188 depth_buf->set_wrap(CLAMP_TO_EDGE);
189 depth_buf->storage(DEPTH_COMPONENT, width, height);
190 fbo->attach(DEPTH_ATTACHMENT, *depth_buf, 0);
194 fbo_ms = new Framebuffer;
196 color_buf_ms = new Renderbuffer;
197 color_buf_ms->storage_multisample(samples, (hdr ? RGB16F : RGB), width, height);
198 fbo_ms->attach(COLOR_ATTACHMENT0, *color_buf_ms);
200 depth_buf_ms = new Renderbuffer;
201 depth_buf_ms->storage_multisample(samples, DEPTH_COMPONENT, width, height);
202 fbo_ms->attach(DEPTH_ATTACHMENT, *depth_buf_ms);
207 Pipeline::Pass::Pass():
213 void Pipeline::Pass::set_lighting(const Lighting *l)
218 void Pipeline::Pass::set_depth_test(const DepthTest *d)
223 void Pipeline::Pass::set_blend(const Blend *b)
229 Pipeline::Slot::Slot(const Renderable *r):