]> git.tdb.fi Git - libs/gl.git/blob - source/pipeline.cpp
Handle clipping in Pipeline and Renderer
[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));
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 void Pipeline::add_postprocessor(PostProcessor &pp)
121 {
122         postproc.push_back(&pp);
123         try
124         {
125                 create_targets(0);
126         }
127         catch(...)
128         {
129                 postproc.pop_back();
130                 throw;
131         }
132 }
133
134 void Pipeline::setup_frame() const
135 {
136         in_frame = true;
137         for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
138                 i->renderable->setup_frame();
139 }
140
141 void Pipeline::finish_frame() const
142 {
143         in_frame = false;
144         for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
145                 i->renderable->finish_frame();
146 }
147
148 void Pipeline::render(const Tag &tag) const
149 {
150         if(tag.id)
151                 return;
152
153         Renderer renderer(camera);
154         render(renderer, tag);
155 }
156
157 void Pipeline::render(Renderer &renderer, const Tag &tag) const
158 {
159         if(tag.id)
160                 return;
161
162         bool was_in_frame = in_frame;
163         if(!in_frame)
164                 setup_frame();
165
166         const Framebuffer *out_fbo = Framebuffer::current();
167         // XXX Make sure the correct FBO is restored if an exception is thrown
168
169         if(target[0])
170         {
171                 Framebuffer &fbo = (samples ? target_ms->fbo : target[0]->fbo);
172                 fbo.bind();
173                 fbo.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
174         }
175
176         for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
177         {
178                 Bind bind_depth_test(i->get_depth_test());
179                 Bind bind_blend(i->get_blend());
180                 renderer.set_lighting(i->get_lighting());
181                 renderer.set_clipping(i->get_clipping());
182
183                 for(vector<Slot>::const_iterator j=renderables.begin(); j!=renderables.end(); ++j)
184                         if(j->passes.empty() || j->passes.count(i->get_tag()))
185                                 renderer.render(*j->renderable, i->get_tag());
186         }
187
188         renderer.end();
189
190         if(target[0])
191         {
192                 if(samples)
193                         target[0]->fbo.blit_from(target_ms->fbo, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false);
194
195                 for(unsigned i=0; i<postproc.size(); ++i)
196                 {
197                         unsigned j = i%2;
198                         if(i+1<postproc.size())
199                                 target[1-j]->fbo.bind();
200                         else
201                                 out_fbo->bind();
202                         postproc[i]->render(target[j]->color, target[j]->depth);
203                 }
204
205                 out_fbo->bind();
206         }
207
208         if(!was_in_frame)
209                 finish_frame();
210 }
211
212 void Pipeline::create_targets(unsigned recreate)
213 {
214         if(recreate>=2)
215         {
216                 delete target[0];
217                 delete target[1];
218                 target[0] = 0;
219                 target[1] = 0;
220         }
221         if(recreate>=1)
222         {
223                 delete target_ms;
224                 target_ms = 0;
225         }
226
227         PixelFormat fmt = (hdr ? RGB16F : RGB);
228         if(!postproc.empty() || samples)
229         {
230                 if(!target[0])
231                         target[0] = new RenderTarget(width, height, fmt);
232                 if(!target[1] && postproc.size()>1)
233                         target[1] = new RenderTarget(width, height, fmt);
234         }
235
236         if(!target_ms && samples)
237                 target_ms = new MultisampleTarget(width, height, samples, fmt);
238 }
239
240
241 Pipeline::Pass::Pass(const Tag &t):
242         tag(t),
243         lighting(0),
244         depth_test(0),
245         blend(0),
246         clipping(0)
247 { }
248
249 void Pipeline::Pass::set_lighting(const Lighting *l)
250 {
251         lighting = l;
252 }
253
254 void Pipeline::Pass::set_depth_test(const DepthTest *d)
255 {
256         depth_test = d;
257 }
258
259 void Pipeline::Pass::set_blend(const Blend *b)
260 {
261         blend = b;
262 }
263
264 void Pipeline::Pass::set_clipping(const Clipping *c)
265 {
266         clipping =c;
267 }
268
269
270 Pipeline::Slot::Slot(const Renderable *r):
271         renderable(r)
272 { }
273
274
275 Pipeline::RenderTarget::RenderTarget(unsigned w, unsigned h, PixelFormat f)
276 {
277         color.set_min_filter(NEAREST);
278         color.set_mag_filter(NEAREST);
279         color.set_wrap(CLAMP_TO_EDGE);
280         color.storage(f, w, h);
281         fbo.attach(COLOR_ATTACHMENT0, color, 0);
282
283         depth.set_min_filter(NEAREST);
284         depth.set_mag_filter(NEAREST);
285         depth.set_wrap(CLAMP_TO_EDGE);
286         depth.storage(DEPTH_COMPONENT, w, h);
287         fbo.attach(DEPTH_ATTACHMENT, depth, 0);
288
289         fbo.require_complete();
290 }
291
292
293 Pipeline::MultisampleTarget::MultisampleTarget(unsigned w, unsigned h, unsigned s, PixelFormat f)
294 {
295         color.storage_multisample(s, f, w, h);
296         fbo.attach(COLOR_ATTACHMENT0, color);
297
298         depth.storage_multisample(s, DEPTH_COMPONENT, w, h);
299         fbo.attach(DEPTH_ATTACHMENT, depth);
300
301         fbo.require_complete();
302 }
303
304 } // namespace GL
305 } // namespace Msp