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