]> git.tdb.fi Git - libs/gl.git/blob - source/pipeline.cpp
Use RAII to restore the output FBO in Pipeline::render
[libs/gl.git] / source / pipeline.cpp
1 #include <msp/core/maputils.h>
2 #include "blend.h"
3 #include "camera.h"
4 #include "framebuffer.h"
5 #include "lighting.h"
6 #include "pipeline.h"
7 #include "postprocessor.h"
8 #include "renderbuffer.h"
9 #include "renderer.h"
10 #include "tests.h"
11 #include "texture2d.h"
12
13 using namespace std;
14
15 namespace Msp {
16 namespace GL {
17
18 Pipeline::Pipeline(unsigned w, unsigned h, bool d):
19         camera(0),
20         width(w),
21         height(h),
22         hdr(d),
23         samples(0),
24         target_ms(0),
25         in_frame(false)
26 {
27         target[0] = 0;
28         target[1] = 0;
29 }
30
31 Pipeline::~Pipeline()
32 {
33         delete target[0];
34         delete target[1];
35         delete target_ms;
36 }
37
38 void Pipeline::set_hdr(bool h)
39 {
40         if(h==hdr)
41                 return;
42
43         bool old_hdr= hdr;
44         hdr = h;
45         try
46         {
47                 create_targets(2);
48         }
49         catch(...)
50         {
51                 hdr = old_hdr;
52                 throw;
53         }
54 }
55
56 void Pipeline::set_multisample(unsigned s)
57 {
58         if(s==samples)
59                 return;
60
61         unsigned old_samples = samples;
62         samples = s;
63         try
64         {
65                 create_targets(1);
66         }
67         catch(...)
68         {
69                 samples = old_samples;
70                 throw;
71         }
72 }
73
74 void Pipeline::set_camera(const Camera *c)
75 {
76         camera = c;
77 }
78
79 Pipeline::Pass &Pipeline::add_pass(const Tag &tag)
80 {
81         passes.push_back(Pass(tag, 0));
82         return passes.back();
83 }
84
85 void Pipeline::add_renderable(const Renderable &r)
86 {
87         for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
88                 if(i->renderable==&r)
89                 {
90                         i->passes.clear();
91                         return;
92                 }
93
94         renderables.push_back(&r);
95 }
96
97 void Pipeline::add_renderable_for_pass(const Renderable &r, const Tag &tag)
98 {
99         for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
100                 if(i->renderable==&r)
101                 {
102                         i->passes.insert(tag);
103                         return;
104                 }
105
106         renderables.push_back(&r);
107         renderables.back().passes.insert(tag);
108 }
109
110 void Pipeline::remove_renderable(const Renderable &r)
111 {
112         for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
113                 if(i->renderable==&r)
114                 {
115                         renderables.erase(i);
116                         return;
117                 }
118 }
119
120 Pipeline::Pass &Pipeline::add_pass(const Tag &tag, const Renderable &r)
121 {
122         passes.push_back(Pass(tag, &r));
123         return passes.back();
124 }
125
126 void Pipeline::add_postprocessor(PostProcessor &pp)
127 {
128         postproc.push_back(&pp);
129         try
130         {
131                 create_targets(0);
132         }
133         catch(...)
134         {
135                 postproc.pop_back();
136                 throw;
137         }
138 }
139
140 void Pipeline::setup_frame() const
141 {
142         in_frame = true;
143         for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
144                 if(const Renderable *renderable = i->get_renderable())
145                         renderable->setup_frame();
146         for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
147                 i->renderable->setup_frame();
148 }
149
150 void Pipeline::finish_frame() const
151 {
152         in_frame = false;
153         for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
154                 if(const Renderable *renderable = i->get_renderable())
155                         renderable->finish_frame();
156         for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
157                 i->renderable->finish_frame();
158 }
159
160 void Pipeline::render(const Tag &tag) const
161 {
162         if(tag.id)
163                 return;
164
165         Renderer renderer(camera);
166         render(renderer, tag);
167 }
168
169 void Pipeline::render(Renderer &renderer, const Tag &tag) const
170 {
171         if(tag.id)
172                 return;
173
174         bool was_in_frame = in_frame;
175         if(!in_frame)
176                 setup_frame();
177
178         const Framebuffer *out_fbo = Framebuffer::current();
179         // This is a no-op but will ensure the FBO binding gets restored
180         BindRestore restore_fbo(out_fbo);
181
182         if(target[0])
183         {
184                 Framebuffer &fbo = (samples ? target_ms->fbo : target[0]->fbo);
185                 fbo.bind();
186                 fbo.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
187         }
188
189         for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
190         {
191                 Bind bind_depth_test(i->get_depth_test());
192                 Bind bind_blend(i->get_blend());
193                 renderer.set_lighting(i->get_lighting());
194                 renderer.set_clipping(i->get_clipping());
195
196                 if(const Renderable *renderable = i->get_renderable())
197                         renderer.render(*renderable, i->get_tag());
198
199                 for(vector<Slot>::const_iterator j=renderables.begin(); j!=renderables.end(); ++j)
200                         if(j->passes.empty() || j->passes.count(i->get_tag()))
201                                 renderer.render(*j->renderable, i->get_tag());
202         }
203
204         renderer.end();
205
206         if(target[0])
207         {
208                 if(samples)
209                         target[0]->fbo.blit_from(target_ms->fbo, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false);
210
211                 for(unsigned i=0; i<postproc.size(); ++i)
212                 {
213                         unsigned j = i%2;
214                         if(i+1<postproc.size())
215                                 target[1-j]->fbo.bind();
216                         else
217                                 out_fbo->bind();
218                         postproc[i]->render(target[j]->color, target[j]->depth);
219                 }
220         }
221
222         if(!was_in_frame)
223                 finish_frame();
224 }
225
226 void Pipeline::create_targets(unsigned recreate)
227 {
228         if(recreate>=2)
229         {
230                 delete target[0];
231                 delete target[1];
232                 target[0] = 0;
233                 target[1] = 0;
234         }
235         if(recreate>=1)
236         {
237                 delete target_ms;
238                 target_ms = 0;
239         }
240
241         PixelFormat fmt = (hdr ? RGB16F : RGB);
242         if(!postproc.empty() || samples)
243         {
244                 if(!target[0])
245                         target[0] = new RenderTarget(width, height, fmt);
246                 if(!target[1] && postproc.size()>1)
247                         target[1] = new RenderTarget(width, height, fmt);
248         }
249
250         if(!target_ms && samples)
251                 target_ms = new MultisampleTarget(width, height, samples, fmt);
252 }
253
254
255 Pipeline::Pass::Pass(const Tag &t, const Renderable *r):
256         tag(t),
257         lighting(0),
258         depth_test(0),
259         blend(0),
260         clipping(0),
261         renderable(r)
262 { }
263
264 void Pipeline::Pass::set_lighting(const Lighting *l)
265 {
266         lighting = l;
267 }
268
269 void Pipeline::Pass::set_depth_test(const DepthTest *d)
270 {
271         depth_test = d;
272 }
273
274 void Pipeline::Pass::set_blend(const Blend *b)
275 {
276         blend = b;
277 }
278
279 void Pipeline::Pass::set_clipping(const Clipping *c)
280 {
281         clipping =c;
282 }
283
284
285 Pipeline::Slot::Slot(const Renderable *r):
286         renderable(r)
287 { }
288
289
290 Pipeline::RenderTarget::RenderTarget(unsigned w, unsigned h, PixelFormat f)
291 {
292         color.set_min_filter(NEAREST);
293         color.set_mag_filter(NEAREST);
294         color.set_wrap(CLAMP_TO_EDGE);
295         color.storage(f, w, h);
296         fbo.attach(COLOR_ATTACHMENT0, color, 0);
297
298         depth.set_min_filter(NEAREST);
299         depth.set_mag_filter(NEAREST);
300         depth.set_wrap(CLAMP_TO_EDGE);
301         depth.storage(DEPTH_COMPONENT, w, h);
302         fbo.attach(DEPTH_ATTACHMENT, depth, 0);
303
304         fbo.require_complete();
305 }
306
307
308 Pipeline::MultisampleTarget::MultisampleTarget(unsigned w, unsigned h, unsigned s, PixelFormat f)
309 {
310         color.storage_multisample(s, f, w, h);
311         fbo.attach(COLOR_ATTACHMENT0, color);
312
313         depth.storage_multisample(s, DEPTH_COMPONENT, w, h);
314         fbo.attach(DEPTH_ATTACHMENT, depth);
315
316         fbo.require_complete();
317 }
318
319 } // namespace GL
320 } // namespace Msp