]> git.tdb.fi Git - libs/gl.git/blob - source/render/sequence.cpp
Miscellaneous cleanup
[libs/gl.git] / source / render / sequence.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 "postprocessor.h"
7 #include "renderbuffer.h"
8 #include "renderer.h"
9 #include "sequence.h"
10 #include "texture2d.h"
11 #include "view.h"
12
13 using namespace std;
14
15 namespace Msp {
16 namespace GL {
17
18 Sequence::Sequence(unsigned w, unsigned h, bool d)
19 {
20         init(w, h);
21         hdr = d;
22 }
23
24 Sequence::Sequence(const View &view)
25 {
26         init(view.get_width(), view.get_height());
27 }
28
29 Sequence::Sequence(const Framebuffer &fbo)
30 {
31         init(fbo.get_width(), fbo.get_height());
32 }
33
34 void Sequence::init(unsigned w, unsigned h)
35 {
36         camera = 0;
37         width = w;
38         height = h;
39         hdr = false;
40         alpha = false;
41         samples = 0;
42         target_ms = 0;
43         target[0] = 0;
44         target[1] = 0;
45 }
46
47 Sequence::~Sequence()
48 {
49         for(vector<PostProcStep>::iterator i=postproc.begin(); i!=postproc.end(); ++i)
50                 if(i->owned)
51                         delete i->postproc;
52         delete target[0];
53         delete target[1];
54         delete target_ms;
55 }
56
57 void Sequence::set_hdr(bool h)
58 {
59         if(h==hdr)
60                 return;
61
62         bool old_hdr = hdr;
63         hdr = h;
64         try
65         {
66                 create_targets(2);
67         }
68         catch(...)
69         {
70                 hdr = old_hdr;
71                 throw;
72         }
73 }
74
75 void Sequence::set_alpha(bool a)
76 {
77         if(a==alpha)
78                 return;
79
80         bool old_alpha = alpha;
81         alpha = a;
82         try
83         {
84                 create_targets(2);
85         }
86         catch(...)
87         {
88                 alpha = old_alpha;
89                 throw;
90         }
91 }
92
93 void Sequence::set_multisample(unsigned s)
94 {
95         if(s==samples)
96                 return;
97
98         unsigned old_samples = samples;
99         samples = s;
100         try
101         {
102                 create_targets(1);
103         }
104         catch(...)
105         {
106                 samples = old_samples;
107                 throw;
108         }
109 }
110
111 Sequence::Step &Sequence::add_step(Tag tag, Renderable &r)
112 {
113         steps.push_back(Step(tag, &r));
114         return steps.back();
115 }
116
117 void Sequence::add_postprocessor(PostProcessor &pp)
118 {
119         add_postprocessor(&pp, false);
120 }
121
122 void Sequence::add_postprocessor_owned(PostProcessor *pp)
123 {
124         add_postprocessor(pp, true);
125 }
126
127 void Sequence::add_postprocessor(PostProcessor *pp, bool owned)
128 {
129         postproc.push_back(PostProcStep(pp, owned));
130         try
131         {
132                 create_targets(0);
133         }
134         catch(...)
135         {
136                 if(!owned)
137                         delete pp;
138                 postproc.pop_back();
139                 throw;
140         }
141 }
142
143 void Sequence::setup_frame(Renderer &renderer)
144 {
145         for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
146                 if(Renderable *renderable = i->get_renderable())
147                         renderable->setup_frame(renderer);
148 }
149
150 void Sequence::finish_frame()
151 {
152         for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
153                 if(Renderable *renderable = i->get_renderable())
154                         renderable->finish_frame();
155 }
156
157 void Sequence::render(Renderer &renderer, Tag tag) const
158 {
159         if(tag.id)
160                 return;
161
162         Renderer::Push _push(renderer);
163
164         const Framebuffer *out_fbo = renderer.get_framebuffer();
165
166         if(target[0])
167         {
168                 renderer.set_framebuffer(&(samples ? target_ms : target[0])->get_framebuffer());
169                 renderer.clear();
170         }
171
172         for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
173         {
174                 Renderer::Push _push2(renderer);
175
176                 renderer.set_depth_test(&i->get_depth_test());
177                 renderer.set_stencil_test(&i->get_stencil_test());
178                 renderer.set_blend(&i->get_blend());
179
180                 if (const Lighting *lighting = i->get_lighting())
181                         renderer.add_shader_data(lighting->get_shader_data());
182                 renderer.set_clipping(i->get_clipping());
183
184                 if(const Renderable *renderable = i->get_renderable())
185                         renderer.render(*renderable, i->get_tag());
186         }
187
188         if(target[0])
189         {
190                 if(samples)
191                         renderer.resolve_multisample(target[0]->get_framebuffer(), COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
192
193                 renderer.set_depth_test(0);
194                 renderer.set_stencil_test(0);
195                 renderer.set_blend(0);
196
197                 for(unsigned i=0; i<postproc.size(); ++i)
198                 {
199                         unsigned j = i%2;
200                         renderer.set_framebuffer(i+1<postproc.size() ? &target[1-j]->get_framebuffer() : out_fbo);
201                         const Texture2D &color = target[j]->get_target_texture(RENDER_COLOR);
202                         const Texture2D &depth = target[j]->get_target_texture(RENDER_DEPTH);
203                         postproc[i].postproc->render(renderer, color, depth);
204                 }
205         }
206 }
207
208 void Sequence::create_targets(unsigned recreate)
209 {
210         if(recreate>=2)
211         {
212                 delete target[0];
213                 delete target[1];
214                 target[0] = 0;
215                 target[1] = 0;
216         }
217         if(recreate>=1)
218         {
219                 delete target_ms;
220                 target_ms = 0;
221         }
222
223         PixelFormat color_pf = (hdr ? (alpha ? RGBA16F : RGB16F) : (alpha ? RGBA8 : RGB8));
224         RenderTargetFormat fmt = (RENDER_COLOR,color_pf, RENDER_DEPTH);
225         if(!postproc.empty() || samples)
226         {
227                 if(!target[0])
228                         target[0] = new RenderTarget(width, height, fmt);
229                 if(!target[1] && postproc.size()>1)
230                         target[1] = new RenderTarget(width, height, fmt);
231         }
232
233         if(!target_ms && samples)
234                 target_ms = new RenderTarget(width, height, samples, fmt);
235
236 #ifdef DEBUG
237         if(!debug_name.empty())
238                 set_target_debug_names();
239 #endif
240 }
241
242 void Sequence::set_debug_name(const string &name)
243 {
244 #ifdef DEBUG
245         debug_name = name;
246         if(!name.empty())
247                 set_target_debug_names();
248 #else
249         (void)name;
250 #endif
251 }
252
253 void Sequence::set_target_debug_names()
254 {
255 #ifdef DEBUG
256         for(unsigned i=0; i<2; ++i)
257                 if(target[i])
258                         target[i]->set_debug_name(format("%s [RT:%d]", debug_name, i));
259         if(target_ms)
260                 target_ms->set_debug_name(debug_name+" [RT:ms]");
261 #endif
262 }
263
264
265 Sequence::Step::Step(Tag t, Renderable *r):
266         tag(t),
267         lighting(0),
268         clipping(0),
269         renderable(r)
270 { }
271
272 void Sequence::Step::set_lighting(const Lighting *l)
273 {
274         lighting = l;
275 }
276
277 void Sequence::Step::set_depth_test(const DepthTest &dt)
278 {
279         depth_test = dt;
280 }
281
282 void Sequence::Step::set_stencil_test(const StencilTest &st)
283 {
284         stencil_test = st;
285 }
286
287 void Sequence::Step::set_blend(const Blend &b)
288 {
289         blend = b;
290 }
291
292 void Sequence::Step::set_clipping(const Clipping *c)
293 {
294         clipping = c;
295 }
296
297 } // namespace GL
298 } // namespace Msp