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