]> git.tdb.fi Git - libs/gl.git/blob - source/render/sequence.cpp
Redesign depth and stencil test and blend state management
[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
167         if(target[0])
168         {
169                 Framebuffer &fbo = (samples ? target_ms : target[0])->get_framebuffer();
170                 fbo.bind();
171                 fbo.clear();
172         }
173
174         for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
175         {
176                 Renderer::Push push(renderer);
177
178                 renderer.set_depth_test(&i->get_depth_test());
179                 renderer.set_stencil_test(&i->get_stencil_test());
180                 renderer.set_blend(&i->get_blend());
181
182                 if (const Lighting *lighting = i->get_lighting())
183                         renderer.add_shader_data(lighting->get_shader_data());
184                 renderer.set_clipping(i->get_clipping());
185
186                 if(const Renderable *renderable = i->get_renderable())
187                         renderer.render(*renderable, i->get_tag());
188         }
189
190         if(target[0])
191         {
192                 renderer.set_depth_test(0);
193                 renderer.set_stencil_test(0);
194                 renderer.set_blend(0);
195
196                 if(samples)
197                         target[0]->blit_from(*target_ms);
198
199                 for(unsigned i=0; i<postproc.size(); ++i)
200                 {
201                         unsigned j = i%2;
202                         if(i+1<postproc.size())
203                                 target[1-j]->get_framebuffer().bind();
204                         else
205                                 out_fbo->bind();
206                         const Texture2D &color = target[j]->get_target_texture(RENDER_COLOR);
207                         const Texture2D &depth = target[j]->get_target_texture(RENDER_DEPTH);
208                         postproc[i].postproc->render(renderer, color, depth);
209                 }
210         }
211 }
212
213 void Sequence::create_targets(unsigned recreate)
214 {
215         if(recreate>=2)
216         {
217                 delete target[0];
218                 delete target[1];
219                 target[0] = 0;
220                 target[1] = 0;
221         }
222         if(recreate>=1)
223         {
224                 delete target_ms;
225                 target_ms = 0;
226         }
227
228         PixelFormat color_pf = (hdr ? (alpha ? RGBA16F : RGB16F) : (alpha ? RGBA8 : RGB8));
229         RenderTargetFormat fmt = (RENDER_COLOR,color_pf, RENDER_DEPTH);
230         if(!postproc.empty() || samples)
231         {
232                 if(!target[0])
233                         target[0] = new RenderTarget(width, height, fmt);
234                 if(!target[1] && postproc.size()>1)
235                         target[1] = new RenderTarget(width, height, fmt);
236         }
237
238         if(!target_ms && samples)
239                 target_ms = new RenderTarget(width, height, samples, fmt);
240
241 #ifdef DEBUG
242         if(!debug_name.empty())
243                 set_target_debug_names();
244 #endif
245 }
246
247 void Sequence::set_debug_name(const string &name)
248 {
249 #ifdef DEBUG
250         debug_name = name;
251         if(!name.empty())
252                 set_target_debug_names();
253 #else
254         (void)name;
255 #endif
256 }
257
258 void Sequence::set_target_debug_names()
259 {
260 #ifdef DEBUG
261         for(unsigned i=0; i<2; ++i)
262                 if(target[i])
263                         target[i]->set_debug_name(format("%s [RT:%d]", debug_name, i));
264         if(target_ms)
265                 target_ms->set_debug_name(debug_name+" [RT:ms]");
266 #endif
267 }
268
269
270 Sequence::Step::Step(Tag t, Renderable *r):
271         tag(t),
272         lighting(0),
273         clipping(0),
274         renderable(r)
275 { }
276
277 void Sequence::Step::set_lighting(const Lighting *l)
278 {
279         lighting = l;
280 }
281
282 void Sequence::Step::set_depth_test(const DepthTest &dt)
283 {
284         depth_test = dt;
285 }
286
287 void Sequence::Step::set_stencil_test(const StencilTest &st)
288 {
289         stencil_test = st;
290 }
291
292 void Sequence::Step::set_blend(const Blend &b)
293 {
294         blend = b;
295 }
296
297 void Sequence::Step::set_clipping(const Clipping *c)
298 {
299         clipping =c;
300 }
301
302 } // namespace GL
303 } // namespace Msp