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