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, 0);
123 color_buf->image(0, RGB, UNSIGNED_BYTE, 0);
124 fbo->attach(COLOR_ATTACHMENT0, *color_buf, 0);
125 depth_buf = new Renderbuffer;
126 depth_buf->storage(DEPTH_COMPONENT, width, height);
127 fbo->attach(DEPTH_ATTACHMENT, *depth_buf);
131 void Pipeline::render(const Tag &tag) const
133 const PipelinePass &pass = get_pass(tag);
135 Bind bind_depth_test(pass.depth_test);
136 Bind bind_blend(pass.blend);
137 Bind bind_lighting(pass.lighting);
139 for(vector<Effect *>::const_iterator i=pass.effects.begin(); i!=pass.effects.end(); ++i)
142 for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
143 if(i->passes.empty() || i->passes.count(tag))
144 i->renderable->render(tag);
146 for(vector<Effect *>::const_iterator i=pass.effects.end(); i!=pass.effects.begin();)
150 void Pipeline::render_all() const
158 fbo->clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
161 for(vector<Effect *>::const_iterator i=effects.begin(); i!=effects.end(); ++i)
164 for(vector<Tag>::const_iterator i=pass_order.begin(); i!=pass_order.end(); ++i)
167 for(vector<Effect *>::const_iterator i=effects.end(); i!=effects.begin();)
171 Framebuffer::unbind();
173 // XXX Need two color buffer textures to handle multiple post-processors correctly
174 for(vector<PostProcessor *>::const_iterator i=postproc.begin(); i!=postproc.end(); ++i)
175 (*i)->render(*color_buf);
179 Pipeline::Slot::Slot(const Renderable *r):