]> git.tdb.fi Git - libs/gl.git/blob - source/pipeline.cpp
Improve the ambient occlusion algorithm
[libs/gl.git] / source / pipeline.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2009-2010  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include "blend.h"
9 #include "camera.h"
10 #include "effect.h"
11 #include "except.h"
12 #include "framebuffer.h"
13 #include "lighting.h"
14 #include "pipeline.h"
15 #include "postprocessor.h"
16 #include "renderbuffer.h"
17 #include "tests.h"
18 #include "texture2d.h"
19
20 using namespace std;
21
22 namespace Msp {
23 namespace GL {
24
25 Pipeline::Pipeline(unsigned w, unsigned h, bool d):
26         camera(0),
27         width(w),
28         height(h),
29         hdr(d),
30         fbo(0),
31         color_buf(0),
32         depth_buf(0)
33 { }
34
35 Pipeline::~Pipeline()
36 {
37         delete fbo;
38         delete color_buf;
39         delete depth_buf;
40 }
41
42 void Pipeline::set_camera(const Camera *c)
43 {
44         camera = c;
45 }
46
47 PipelinePass &Pipeline::add_pass(const Tag &tag)
48 {
49         if(passes.count(tag))
50                 throw KeyError("Pass already exists");
51
52         PipelinePass &pass = passes[tag];
53         pass_order.push_back(tag);
54         return pass;
55 }
56
57 PipelinePass &Pipeline::get_pass(const Tag &tag)
58 {
59         PassMap::iterator i = passes.find(tag);
60         if(i==passes.end())
61                 throw KeyError("Unknown pass");
62         return i->second;
63 }
64
65 const PipelinePass &Pipeline::get_pass(const Tag &tag) const
66 {
67         PassMap::const_iterator i = passes.find(tag);
68         if(i==passes.end())
69                 throw KeyError("Unknown pass");
70         return i->second;
71 }
72
73 void Pipeline::add_renderable(const Renderable &r)
74 {
75         for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
76                 if(i->renderable==&r)
77                 {
78                         i->passes.clear();
79                         return;
80                 }
81
82         renderables.push_back(&r);
83 }
84
85 void Pipeline::add_renderable_for_pass(const Renderable &r, const Tag &tag)
86 {
87         for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
88                 if(i->renderable==&r)
89                 {
90                         i->passes.insert(tag);
91                         return;
92                 }
93
94         renderables.push_back(&r);
95         renderables.back().passes.insert(tag);
96 }
97
98 void Pipeline::remove_renderable(const Renderable &r)
99 {
100         for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
101                 if(i->renderable==&r)
102                 {
103                         renderables.erase(i);
104                         return;
105                 }
106 }
107
108 void Pipeline::add_effect(Effect &e)
109 {
110         effects.push_back(&e);
111 }
112
113 void Pipeline::add_postprocessor(PostProcessor &pp)
114 {
115         postproc.push_back(&pp);
116         if(!fbo)
117         {
118                 fbo = new Framebuffer;
119
120                 color_buf = new Texture2D;
121                 color_buf->set_min_filter(NEAREST);
122                 color_buf->set_mag_filter(NEAREST);
123                 color_buf->set_wrap(CLAMP_TO_EDGE);
124                 color_buf->storage((hdr ? RGB16F : RGB), width, height);
125                 fbo->attach(COLOR_ATTACHMENT0, *color_buf, 0);
126
127                 depth_buf = new Texture2D;
128                 depth_buf->set_min_filter(NEAREST);
129                 depth_buf->set_mag_filter(NEAREST);
130                 depth_buf->set_wrap(CLAMP_TO_EDGE);
131                 depth_buf->storage(DEPTH_COMPONENT, width, height);
132                 fbo->attach(DEPTH_ATTACHMENT, *depth_buf, 0);
133         }
134 }
135
136 void Pipeline::render(const Tag &tag) const
137 {
138         const PipelinePass &pass = get_pass(tag);
139
140         Bind bind_depth_test(pass.depth_test);
141         Bind bind_blend(pass.blend);
142         Bind bind_lighting(pass.lighting);
143
144         for(vector<Effect *>::const_iterator i=pass.effects.begin(); i!=pass.effects.end(); ++i)
145                 (*i)->prepare();
146
147         for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
148                 if(i->passes.empty() || i->passes.count(tag))
149                         i->renderable->render(tag);
150
151         for(vector<Effect *>::const_iterator i=pass.effects.end(); i!=pass.effects.begin();)
152                 (*--i)->cleanup();
153 }
154
155 void Pipeline::render_all() const
156 {
157         if(camera)
158                 camera->apply();
159
160         if(fbo)
161         {
162                 fbo->bind();
163                 fbo->clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
164         }
165
166         for(vector<Effect *>::const_iterator i=effects.begin(); i!=effects.end(); ++i)
167                 (*i)->prepare();
168
169         for(vector<Tag>::const_iterator i=pass_order.begin(); i!=pass_order.end(); ++i)
170                 render(*i);
171
172         for(vector<Effect *>::const_iterator i=effects.end(); i!=effects.begin();)
173                 (*--i)->cleanup();
174
175         if(fbo)
176                 Framebuffer::unbind();
177
178         // XXX Need two color buffer textures to handle multiple post-processors correctly
179         for(vector<PostProcessor *>::const_iterator i=postproc.begin(); i!=postproc.end(); ++i)
180                 (*i)->render(*color_buf, *depth_buf);
181 }
182
183
184 Pipeline::Slot::Slot(const Renderable *r):
185         renderable(r)
186 { }
187
188 } // namespace GL
189 } // namespace Msp