]> git.tdb.fi Git - libs/gl.git/blob - source/render/sequence.cpp
Deprecated setting Material or Lighting on Renderer
[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                 if (const Lighting *lighting = i->get_lighting())
189                         renderer.add_shader_data(lighting->get_shader_data());
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
196         if(target[0])
197         {
198                 DepthTest::unbind();
199                 Blend::unbind();
200
201                 if(samples)
202                         target[0]->blit_from(*target_ms);
203
204                 for(unsigned i=0; i<postproc.size(); ++i)
205                 {
206                         unsigned j = i%2;
207                         if(i+1<postproc.size())
208                                 target[1-j]->get_framebuffer().bind();
209                         else
210                                 out_fbo->bind();
211                         const Texture2D &color = target[j]->get_target_texture(RENDER_COLOR);
212                         const Texture2D &depth = target[j]->get_target_texture(RENDER_DEPTH);
213                         postproc[i].postproc->render(renderer, color, depth);
214                 }
215         }
216 }
217
218 void Sequence::create_targets(unsigned recreate)
219 {
220         if(recreate>=2)
221         {
222                 delete target[0];
223                 delete target[1];
224                 target[0] = 0;
225                 target[1] = 0;
226         }
227         if(recreate>=1)
228         {
229                 delete target_ms;
230                 target_ms = 0;
231         }
232
233         PixelFormat color_pf = (hdr ? (alpha ? RGBA16F : RGB16F) : (alpha ? RGBA8 : RGB8));
234         RenderTargetFormat fmt = (RENDER_COLOR,color_pf, RENDER_DEPTH);
235         if(!postproc.empty() || samples)
236         {
237                 if(!target[0])
238                         target[0] = new RenderTarget(width, height, fmt);
239                 if(!target[1] && postproc.size()>1)
240                         target[1] = new RenderTarget(width, height, fmt);
241         }
242
243         if(!target_ms && samples)
244                 target_ms = new RenderTarget(width, height, samples, fmt);
245
246 #ifdef DEBUG
247         if(!debug_name.empty())
248                 set_target_debug_names();
249 #endif
250 }
251
252 void Sequence::set_debug_name(const string &name)
253 {
254 #ifdef DEBUG
255         debug_name = name;
256         if(!name.empty())
257                 set_target_debug_names();
258 #else
259         (void)name;
260 #endif
261 }
262
263 void Sequence::set_target_debug_names()
264 {
265 #ifdef DEBUG
266         for(unsigned i=0; i<2; ++i)
267                 if(target[i])
268                         target[i]->set_debug_name(format("%s [RT:%d]", debug_name, i));
269         if(target_ms)
270                 target_ms->set_debug_name(debug_name+" [RT:ms]");
271 #endif
272 }
273
274
275 Sequence::Step::Step(Tag t, Renderable *r):
276         tag(t),
277         lighting(0),
278         depth_test(0),
279         blend(0),
280         clipping(0),
281         renderable(r)
282 { }
283
284 void Sequence::Step::set_lighting(const Lighting *l)
285 {
286         lighting = l;
287 }
288
289 void Sequence::Step::set_depth_test(const DepthTest *d)
290 {
291         depth_test = d;
292 }
293
294 void Sequence::Step::set_blend(const Blend *b)
295 {
296         blend = b;
297 }
298
299 void Sequence::Step::set_clipping(const Clipping *c)
300 {
301         clipping =c;
302 }
303
304 } // namespace GL
305 } // namespace Msp