3 This file is part of libmspgl
4 Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
12 #include "framebuffer.h"
15 #include "postprocessor.h"
16 #include "renderbuffer.h"
18 #include "texture2d.h"
25 Pipeline::Pipeline(unsigned w, unsigned h, bool d):
42 void Pipeline::set_camera(const Camera *c)
47 PipelinePass &Pipeline::add_pass(const Tag &tag)
50 throw KeyError("Pass already exists");
52 PipelinePass &pass = passes[tag];
53 pass_order.push_back(tag);
57 PipelinePass &Pipeline::get_pass(const Tag &tag)
59 PassMap::iterator i = passes.find(tag);
61 throw KeyError("Unknown pass");
65 const PipelinePass &Pipeline::get_pass(const Tag &tag) const
67 PassMap::const_iterator i = passes.find(tag);
69 throw KeyError("Unknown pass");
73 void Pipeline::add_renderable(const Renderable &r)
75 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
82 renderables.push_back(&r);
85 void Pipeline::add_renderable_for_pass(const Renderable &r, const Tag &tag)
87 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
90 i->passes.insert(tag);
94 renderables.push_back(&r);
95 renderables.back().passes.insert(tag);
98 void Pipeline::remove_renderable(const Renderable &r)
100 for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
101 if(i->renderable==&r)
103 renderables.erase(i);
108 void Pipeline::add_effect(Effect &e)
110 effects.push_back(&e);
113 void Pipeline::add_postprocessor(PostProcessor &pp)
115 postproc.push_back(&pp);
118 fbo = new Framebuffer;
119 color_buf = new Texture2D;
120 color_buf->set_min_filter(NEAREST);
121 color_buf->set_mag_filter(NEAREST);
122 color_buf->storage((hdr ? RGB16F : RGB), width, height);
123 fbo->attach(COLOR_ATTACHMENT0, *color_buf, 0);
124 depth_buf = new Renderbuffer;
125 depth_buf->storage(DEPTH_COMPONENT, width, height);
126 fbo->attach(DEPTH_ATTACHMENT, *depth_buf);
130 void Pipeline::render(const Tag &tag) const
132 const PipelinePass &pass = get_pass(tag);
134 Bind bind_depth_test(pass.depth_test);
135 Bind bind_blend(pass.blend);
136 Bind bind_lighting(pass.lighting);
138 for(vector<Effect *>::const_iterator i=pass.effects.begin(); i!=pass.effects.end(); ++i)
141 for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
142 if(i->passes.empty() || i->passes.count(tag))
143 i->renderable->render(tag);
145 for(vector<Effect *>::const_iterator i=pass.effects.end(); i!=pass.effects.begin();)
149 void Pipeline::render_all() const
157 fbo->clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
160 for(vector<Effect *>::const_iterator i=effects.begin(); i!=effects.end(); ++i)
163 for(vector<Tag>::const_iterator i=pass_order.begin(); i!=pass_order.end(); ++i)
166 for(vector<Effect *>::const_iterator i=effects.end(); i!=effects.begin();)
170 Framebuffer::unbind();
172 // XXX Need two color buffer textures to handle multiple post-processors correctly
173 for(vector<PostProcessor *>::const_iterator i=postproc.begin(); i!=postproc.end(); ++i)
174 (*i)->render(*color_buf);
178 Pipeline::Slot::Slot(const Renderable *r):