]> git.tdb.fi Git - libs/gl.git/blob - source/pipeline.cpp
Display overall progress when exporting scenes
[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         hdr = h;
44         create_targets(2);
45 }
46
47 void Pipeline::set_multisample(unsigned s)
48 {
49         if(s==samples)
50                 return;
51
52         unsigned old_samples = samples;
53         samples = s;
54         try
55         {
56                 create_targets(1);
57         }
58         catch(...)
59         {
60                 samples = old_samples;
61                 throw;
62         }
63 }
64
65 void Pipeline::set_camera(const Camera *c)
66 {
67         camera = c;
68 }
69
70 Pipeline::Pass &Pipeline::add_pass(const Tag &tag)
71 {
72         passes.push_back(Pass(tag));
73         return passes.back();
74 }
75
76 void Pipeline::add_renderable(const Renderable &r)
77 {
78         for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
79                 if(i->renderable==&r)
80                 {
81                         i->passes.clear();
82                         return;
83                 }
84
85         renderables.push_back(&r);
86 }
87
88 void Pipeline::add_renderable_for_pass(const Renderable &r, const Tag &tag)
89 {
90         for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
91                 if(i->renderable==&r)
92                 {
93                         i->passes.insert(tag);
94                         return;
95                 }
96
97         renderables.push_back(&r);
98         renderables.back().passes.insert(tag);
99 }
100
101 void Pipeline::remove_renderable(const Renderable &r)
102 {
103         for(vector<Slot>::iterator i=renderables.begin(); i!=renderables.end(); ++i)
104                 if(i->renderable==&r)
105                 {
106                         renderables.erase(i);
107                         return;
108                 }
109 }
110
111 void Pipeline::add_postprocessor(PostProcessor &pp)
112 {
113         postproc.push_back(&pp);
114         create_targets(0);
115 }
116
117 void Pipeline::setup_frame() const
118 {
119         in_frame = true;
120         for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
121                 i->renderable->setup_frame();
122 }
123
124 void Pipeline::finish_frame() const
125 {
126         in_frame = false;
127         for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
128                 i->renderable->finish_frame();
129 }
130
131 void Pipeline::render(const Tag &tag) const
132 {
133         if(tag.id)
134                 return;
135
136         Renderer renderer(camera);
137         render(renderer, tag);
138 }
139
140 void Pipeline::render(Renderer &renderer, const Tag &tag) const
141 {
142         if(tag.id)
143                 return;
144
145         bool was_in_frame = in_frame;
146         if(!in_frame)
147                 setup_frame();
148
149         const Framebuffer *out_fbo = Framebuffer::current();
150         // XXX Make sure the correct FBO is restored if an exception is thrown
151
152         if(target[0])
153         {
154                 Framebuffer &fbo = (samples ? target_ms->fbo : target[0]->fbo);
155                 fbo.bind();
156                 fbo.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
157         }
158
159         for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
160         {
161                 Bind bind_depth_test(i->get_depth_test());
162                 Bind bind_blend(i->get_blend());
163                 renderer.set_lighting(i->get_lighting());
164
165                 for(vector<Slot>::const_iterator j=renderables.begin(); j!=renderables.end(); ++j)
166                         if(j->passes.empty() || j->passes.count(i->get_tag()))
167                                 renderer.render(*j->renderable, i->get_tag());
168         }
169
170         renderer.escape();
171
172         if(target[0])
173         {
174                 if(samples)
175                         target[0]->fbo.blit_from(target_ms->fbo, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false);
176
177                 for(unsigned i=0; i<postproc.size(); ++i)
178                 {
179                         unsigned j = i%2;
180                         if(i+1<postproc.size())
181                                 target[1-j]->fbo.bind();
182                         else
183                                 out_fbo->bind();
184                         postproc[i]->render(target[j]->color, target[j]->depth);
185                 }
186
187                 out_fbo->bind();
188         }
189
190         if(!was_in_frame)
191                 finish_frame();
192 }
193
194 void Pipeline::create_targets(unsigned recreate)
195 {
196         if(recreate>=2)
197         {
198                 delete target[0];
199                 delete target[1];
200                 target[0] = 0;
201                 target[1] = 0;
202         }
203         if(recreate>=1)
204         {
205                 delete target_ms;
206                 target_ms = 0;
207         }
208
209         PixelFormat fmt = (hdr ? RGB16F : RGB);
210         if(!postproc.empty() || samples)
211         {
212                 if(!target[0])
213                         target[0] = new RenderTarget(width, height, fmt);
214                 if(!target[1] && postproc.size()>1)
215                         target[1] = new RenderTarget(width, height, fmt);
216         }
217
218         if(!target_ms && samples)
219                 target_ms = new MultisampleTarget(width, height, samples, fmt);
220 }
221
222
223 Pipeline::Pass::Pass(const Tag &t):
224         tag(t),
225         lighting(0),
226         depth_test(0),
227         blend(0)
228 { }
229
230 void Pipeline::Pass::set_lighting(const Lighting *l)
231 {
232         lighting = l;
233 }
234
235 void Pipeline::Pass::set_depth_test(const DepthTest *d)
236 {
237         depth_test = d;
238 }
239
240 void Pipeline::Pass::set_blend(const Blend *b)
241 {
242         blend = b;
243 }
244
245
246 Pipeline::Slot::Slot(const Renderable *r):
247         renderable(r)
248 { }
249
250
251 Pipeline::RenderTarget::RenderTarget(unsigned w, unsigned h, PixelFormat f)
252 {
253         color.set_min_filter(NEAREST);
254         color.set_mag_filter(NEAREST);
255         color.set_wrap(CLAMP_TO_EDGE);
256         color.storage(f, w, h);
257         fbo.attach(COLOR_ATTACHMENT0, color, 0);
258
259         depth.set_min_filter(NEAREST);
260         depth.set_mag_filter(NEAREST);
261         depth.set_wrap(CLAMP_TO_EDGE);
262         depth.storage(DEPTH_COMPONENT, w, h);
263         fbo.attach(DEPTH_ATTACHMENT, depth, 0);
264
265         fbo.require_complete();
266 }
267
268
269 Pipeline::MultisampleTarget::MultisampleTarget(unsigned w, unsigned h, unsigned s, PixelFormat f)
270 {
271         color.storage_multisample(s, f, w, h);
272         fbo.attach(COLOR_ATTACHMENT0, color);
273
274         depth.storage_multisample(s, DEPTH_COMPONENT, w, h);
275         fbo.attach(DEPTH_ATTACHMENT, depth);
276
277         fbo.require_complete();
278 }
279
280 } // namespace GL
281 } // namespace Msp