]> git.tdb.fi Git - libs/gl.git/blob - source/render/pipeline.cpp
21f9e85e76238ea2ccfd2c03b16855511d0503fe
[libs/gl.git] / source / render / 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 Pipeline::Pass &Pipeline::add_pass(Tag tag, Renderable &r)
110 {
111         passes.push_back(Pass(tag, &r));
112         return passes.back();
113 }
114
115 void Pipeline::add_postprocessor(PostProcessor &pp)
116 {
117         add_postprocessor(&pp, true);
118 }
119
120 void Pipeline::add_postprocessor_owned(PostProcessor *pp)
121 {
122         add_postprocessor(pp, false);
123 }
124
125 void Pipeline::add_postprocessor(PostProcessor *pp, bool keep)
126 {
127         postproc.push_back(pp);
128         if(keep)
129                 postproc.back().keep();
130         try
131         {
132                 create_targets(0);
133         }
134         catch(...)
135         {
136                 postproc.pop_back();
137                 throw;
138         }
139 }
140
141 void Pipeline::setup_frame(Renderer &renderer)
142 {
143         for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
144                 if(Renderable *renderable = i->get_renderable())
145                         renderable->setup_frame(renderer);
146         for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
147                 i->renderable->setup_frame(renderer);
148 }
149
150 void Pipeline::finish_frame()
151 {
152         for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
153                 if(Renderable *renderable = i->get_renderable())
154                         renderable->finish_frame();
155         for(vector<Slot>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
156                 i->renderable->finish_frame();
157 }
158
159 void Pipeline::render(Renderer &renderer, Tag tag) const
160 {
161         if(tag.id)
162                 return;
163
164         const Framebuffer *out_fbo = Framebuffer::current();
165         // These are no-ops but will ensure the related state gets restored
166         BindRestore restore_fbo(out_fbo);
167         BindRestore restore_depth_test(DepthTest::current());
168         BindRestore restore_blend(Blend::current());
169
170         if(target[0])
171         {
172                 Framebuffer &fbo = (samples ? target_ms : target[0])->get_framebuffer();
173                 fbo.bind();
174                 fbo.clear();
175         }
176
177         for(PassList::const_iterator i=passes.begin(); i!=passes.end(); ++i)
178         {
179                 if(const DepthTest *dt = i->get_depth_test())
180                         dt->bind();
181                 else
182                         DepthTest::unbind();
183
184                 if(const Blend *b = i->get_blend())
185                         b->bind();
186                 else
187                         Blend::unbind();
188
189                 renderer.set_lighting(i->get_lighting());
190                 renderer.set_clipping(i->get_clipping());
191
192                 if(const Renderable *renderable = i->get_renderable())
193                         renderer.render(*renderable, i->get_tag());
194
195                 for(vector<Slot>::const_iterator j=renderables.begin(); j!=renderables.end(); ++j)
196                         if(j->passes.empty() || j->passes.count(i->get_tag()))
197                                 renderer.render(*j->renderable, i->get_tag());
198         }
199
200         if(target[0])
201         {
202                 DepthTest::unbind();
203                 Blend::unbind();
204
205                 if(samples)
206                         target[0]->blit_from(*target_ms);
207
208                 for(unsigned i=0; i<postproc.size(); ++i)
209                 {
210                         unsigned j = i%2;
211                         if(i+1<postproc.size())
212                                 target[1-j]->get_framebuffer().bind();
213                         else
214                                 out_fbo->bind();
215                         const Texture2D &color = target[j]->get_target_texture(RENDER_COLOR);
216                         const Texture2D &depth = target[j]->get_target_texture(RENDER_DEPTH);
217                         postproc[i]->render(renderer, color, depth);
218                 }
219         }
220 }
221
222 void Pipeline::create_targets(unsigned recreate)
223 {
224         if(recreate>=2)
225         {
226                 delete target[0];
227                 delete target[1];
228                 target[0] = 0;
229                 target[1] = 0;
230         }
231         if(recreate>=1)
232         {
233                 delete target_ms;
234                 target_ms = 0;
235         }
236
237         PixelFormat color_pf = (hdr ? (alpha ? RGBA16F : RGB16F) : (alpha ? RGBA8 : RGB8));
238         RenderTargetFormat fmt = (RENDER_COLOR,color_pf, RENDER_DEPTH);
239         if(!postproc.empty() || samples)
240         {
241                 if(!target[0])
242                         target[0] = new RenderTarget(width, height, fmt);
243                 if(!target[1] && postproc.size()>1)
244                         target[1] = new RenderTarget(width, height, fmt);
245         }
246
247         if(!target_ms && samples)
248                 target_ms = new RenderTarget(width, height, samples, fmt);
249 }
250
251
252 Pipeline::Pass::Pass(Tag t, Renderable *r):
253         tag(t),
254         lighting(0),
255         depth_test(0),
256         blend(0),
257         clipping(0),
258         renderable(r)
259 { }
260
261 void Pipeline::Pass::set_lighting(const Lighting *l)
262 {
263         lighting = l;
264 }
265
266 void Pipeline::Pass::set_depth_test(const DepthTest *d)
267 {
268         depth_test = d;
269 }
270
271 void Pipeline::Pass::set_blend(const Blend *b)
272 {
273         blend = b;
274 }
275
276 void Pipeline::Pass::set_clipping(const Clipping *c)
277 {
278         clipping =c;
279 }
280
281
282 Pipeline::Slot::Slot(Renderable *r):
283         renderable(r)
284 { }
285
286 } // namespace GL
287 } // namespace Msp