1 #include <msp/core/maputils.h>
4 #include "framebuffer.h"
7 #include "postprocessor.h"
8 #include "renderbuffer.h"
11 #include "texture2d.h"
19 Pipeline::Pipeline(unsigned w, unsigned h, bool d)
25 Pipeline::Pipeline(const View &view)
27 init(view.get_width(), view.get_height());
30 Pipeline::Pipeline(const Framebuffer &fbo)
32 init(fbo.get_width(), fbo.get_height());
35 void Pipeline::init(unsigned w, unsigned h)
55 void Pipeline::set_hdr(bool h)
73 void Pipeline::set_alpha(bool a)
78 bool old_alpha = alpha;
91 void Pipeline::set_multisample(unsigned s)
96 unsigned old_samples = samples;
104 samples = old_samples;
109 Pipeline::Pass &Pipeline::add_pass(const Tag &tag, Renderable &r)
111 passes.push_back(Pass(tag, &r));
112 return passes.back();
115 void Pipeline::add_postprocessor(PostProcessor &pp)
117 add_postprocessor(&pp, true);
120 void Pipeline::add_postprocessor_owned(PostProcessor *pp)
122 add_postprocessor(pp, false);
125 void Pipeline::add_postprocessor(PostProcessor *pp, bool keep)
127 postproc.push_back(pp);
129 postproc.back().keep();
141 void Pipeline::setup_frame(Renderer &renderer)
143 for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
144 if(Renderable *renderable = i->get_renderable())
145 renderable->setup_frame(renderer);
146 for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
147 i->renderable->setup_frame(renderer);
150 void Pipeline::finish_frame()
152 for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
153 if(Renderable *renderable = i->get_renderable())
154 renderable->finish_frame();
155 for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
156 i->renderable->finish_frame();
159 void Pipeline::render(Renderer &renderer, const Tag &tag) const
164 const Framebuffer *out_fbo = Framebuffer::current();
165 // These are no-ops but will ensure the related state gets restored
166 BindRestore restore_fbo(out_fbo);
167 BindRestore restore_depth_test(DepthTest::current());
168 BindRestore restore_blend(Blend::current());
172 Framebuffer &fbo = (samples ? target_ms : target[0])->get_framebuffer();
177 for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
179 if(const DepthTest *dt = i->get_depth_test())
184 if(const Blend *b = i->get_blend())
189 renderer.set_lighting(i->get_lighting());
190 renderer.set_clipping(i->get_clipping());
192 if(const Renderable *renderable = i->get_renderable())
193 renderer.render(*renderable, i->get_tag());
195 for(vector<Slot>::const_iterator j=renderables.begin(); j!=renderables.end(); ++j)
196 if(j->passes.empty() || j->passes.count(i->get_tag()))
197 renderer.render(*j->renderable, i->get_tag());
206 target[0]->blit_from(*target_ms);
208 for(unsigned i=0; i<postproc.size(); ++i)
211 if(i+1<postproc.size())
212 target[1-j]->get_framebuffer().bind();
215 const Texture2D &color = target[j]->get_target_texture(RENDER_COLOR);
216 const Texture2D &depth = target[j]->get_target_texture(RENDER_DEPTH);
217 postproc[i]->render(renderer, color, depth);
222 void Pipeline::create_targets(unsigned recreate)
237 PixelFormat color_pf = (hdr ? (alpha ? RGBA16F : RGB16F) : (alpha ? RGBA8 : RGB8));
238 RenderTargetFormat fmt = (RENDER_COLOR,color_pf, RENDER_DEPTH);
239 if(!postproc.empty() || samples)
242 target[0] = new RenderTarget(width, height, fmt);
243 if(!target[1] && postproc.size()>1)
244 target[1] = new RenderTarget(width, height, fmt);
247 if(!target_ms && samples)
248 target_ms = new RenderTarget(width, height, samples, fmt);
252 Pipeline::Pass::Pass(const Tag &t, Renderable *r):
261 void Pipeline::Pass::set_lighting(const Lighting *l)
266 void Pipeline::Pass::set_depth_test(const DepthTest *d)
271 void Pipeline::Pass::set_blend(const Blend *b)
276 void Pipeline::Pass::set_clipping(const Clipping *c)
282 Pipeline::Slot::Slot(Renderable *r):