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