5 #include "framebuffer.h"
8 #include "postprocessor.h"
9 #include "renderbuffer.h"
12 #include "texture2d.h"
19 Pipeline::Pipeline(unsigned w, unsigned h, bool d):
40 void Pipeline::set_hdr(bool h)
47 void Pipeline::set_multisample(unsigned s)
54 void Pipeline::set_camera(const Camera *c)
59 PipelinePass &Pipeline::add_pass(const Tag &tag)
62 throw KeyError("Pass already exists");
64 PipelinePass &pass = passes[tag];
65 pass_order.push_back(tag);
69 PipelinePass &Pipeline::get_pass(const Tag &tag)
71 PassMap::iterator i = passes.find(tag);
73 throw KeyError("Unknown pass");
77 const PipelinePass &Pipeline::get_pass(const Tag &tag) const
79 PassMap::const_iterator i = passes.find(tag);
81 throw KeyError("Unknown pass");
85 void Pipeline::add_renderable(const Renderable &r)
87 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
94 renderables.push_back(&r);
97 void Pipeline::add_renderable_for_pass(const Renderable &r, const Tag &tag)
99 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
100 if(i->renderable==&r)
102 i->passes.insert(tag);
106 renderables.push_back(&r);
107 renderables.back().passes.insert(tag);
110 void Pipeline::remove_renderable(const Renderable &r)
112 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
113 if(i->renderable==&r)
115 renderables.erase(i);
120 void Pipeline::add_effect(Effect &e)
122 effects.push_back(&e);
125 void Pipeline::add_postprocessor(PostProcessor &pp)
127 postproc.push_back(&pp);
134 void Pipeline::render(Renderer &renderer, const Tag &tag) const
136 const PipelinePass &pass = get_pass(tag);
138 Bind bind_depth_test(pass.depth_test);
139 Bind bind_blend(pass.blend);
140 Bind bind_lighting(pass.lighting);
142 for(vector<Effect *>::const_iterator i=pass.effects.begin(); i!=pass.effects.end(); ++i)
145 for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
146 if(i->passes.empty() || i->passes.count(tag))
147 i->renderable->render(renderer, tag);
149 for(vector<Effect *>::const_iterator i=pass.effects.end(); i!=pass.effects.begin();)
153 void Pipeline::render_all() const
160 Framebuffer *f = (fbo_ms ? fbo_ms : fbo);
162 f->clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
165 for(vector<Effect *>::const_iterator i=effects.begin(); i!=effects.end(); ++i)
168 Renderer renderer(camera);
169 for(vector<Tag>::const_iterator i=pass_order.begin(); i!=pass_order.end(); ++i)
170 render(renderer, *i);
172 for(vector<Effect *>::const_iterator i=effects.end(); i!=effects.begin();)
178 fbo->blit_from(*fbo_ms, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false);
179 Framebuffer::unbind();
182 // XXX Need two color buffer textures to handle multiple post-processors correctly
183 for(vector<PostProcessor *>::const_iterator i=postproc.begin(); i!=postproc.end(); ++i)
184 (*i)->render(*color_buf, *depth_buf);
187 void Pipeline::create_fbos()
200 fbo = new Framebuffer;
202 color_buf = new Texture2D;
203 color_buf->set_min_filter(NEAREST);
204 color_buf->set_mag_filter(NEAREST);
205 color_buf->set_wrap(CLAMP_TO_EDGE);
206 color_buf->storage((hdr ? RGB16F : RGB), width, height);
207 fbo->attach(COLOR_ATTACHMENT0, *color_buf, 0);
209 depth_buf = new Texture2D;
210 depth_buf->set_min_filter(NEAREST);
211 depth_buf->set_mag_filter(NEAREST);
212 depth_buf->set_wrap(CLAMP_TO_EDGE);
213 depth_buf->storage(DEPTH_COMPONENT, width, height);
214 fbo->attach(DEPTH_ATTACHMENT, *depth_buf, 0);
218 fbo_ms = new Framebuffer;
220 color_buf_ms = new Renderbuffer;
221 color_buf_ms->storage_multisample(samples, (hdr ? RGB16F : RGB), width, height);
222 fbo_ms->attach(COLOR_ATTACHMENT0, *color_buf_ms);
224 depth_buf_ms = new Renderbuffer;
225 depth_buf_ms->storage_multisample(samples, DEPTH_COMPONENT, width, height);
226 fbo_ms->attach(DEPTH_ATTACHMENT, *depth_buf_ms);
231 Pipeline::Slot::Slot(const Renderable *r):