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