]> git.tdb.fi Git - libs/gl.git/blob - source/render/sequence.cpp
Make it possible to specify explicit clear values
[libs/gl.git] / source / render / sequence.cpp
1 #include <msp/core/maputils.h>
2 #include "blend.h"
3 #include "error.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
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 Sequence::Sequence():
17         width(0),
18         height(0),
19         target{0, 0},
20         target_ms(0),
21         clear_enabled(false),
22         clear_depth(1.0f),
23         clear_stencil(0)
24 { }
25
26 Sequence::Sequence(unsigned w, unsigned h, const FrameFormat &f):
27         width(w),
28         height(h),
29         target_format(f),
30         target_ms(0),
31         clear_enabled(false),
32         clear_depth(1.0f),
33         clear_stencil(0)
34 {
35         if(target_format.empty())
36                 throw invalid_argument("Sequence::Sequence");
37
38         FrameFormat postproc_fmt = target_format;
39         postproc_fmt.set_samples(1);
40         target[0] = new RenderTarget(width, height, postproc_fmt);
41         target[1] = new RenderTarget(width, height, postproc_fmt);
42
43         if(target_format.get_samples()>1)
44                 target_ms = new RenderTarget(width, height, target_format);
45 }
46
47 Sequence::~Sequence()
48 {
49         for(PostProcStep &p: postproc)
50                 if(p.owned)
51                         delete p.postproc;
52         delete target[0];
53         delete target[1];
54         delete target_ms;
55 }
56
57 void Sequence::set_clear_enabled(bool c)
58 {
59         clear_enabled = c;
60 }
61
62 void Sequence::set_clear_colors(const vector<Color> &c)
63 {
64         clear_enabled = true;
65         clear_colors = c;
66 }
67
68 void Sequence::set_clear_depth(float d)
69 {
70         clear_enabled = true;
71         clear_depth = d;
72 }
73
74 void Sequence::set_clear_stencil(int s)
75 {
76         clear_enabled = true;
77         clear_stencil = s;
78 }
79
80 Sequence::Step &Sequence::add_step(Tag tag, Renderable &r)
81 {
82         steps.push_back(Step(tag, &r));
83         return steps.back();
84 }
85
86 void Sequence::add_postprocessor(PostProcessor &pp)
87 {
88         add_postprocessor(&pp, false);
89 }
90
91 void Sequence::add_postprocessor_owned(PostProcessor *pp)
92 {
93         add_postprocessor(pp, true);
94 }
95
96 void Sequence::add_postprocessor(PostProcessor *pp, bool owned)
97 {
98         if(target_format.empty())
99         {
100                 if(owned)
101                         delete pp;
102                 throw invalid_operation("Sequence::add_postprocessor");
103         }
104         postproc.push_back(PostProcStep(pp, owned));
105 }
106
107 void Sequence::setup_frame(Renderer &renderer)
108 {
109         for(const Step &s: steps)
110                 if(Renderable *renderable = s.get_renderable())
111                         renderable->setup_frame(renderer);
112 }
113
114 void Sequence::finish_frame()
115 {
116         for(const Step &s: steps)
117                 if(Renderable *renderable = s.get_renderable())
118                         renderable->finish_frame();
119 }
120
121 void Sequence::render(Renderer &renderer, Tag tag) const
122 {
123         if(tag.id)
124                 return;
125
126         Renderer::Push _push(renderer);
127
128         const Framebuffer *out_fbo = renderer.get_framebuffer();
129
130         if(target[0])
131                 renderer.set_framebuffer(&(target_ms ? target_ms : target[0])->get_framebuffer());
132
133         if(clear_enabled)
134         {
135                 const Framebuffer *target_fbo = renderer.get_framebuffer();
136                 if(!target_fbo)
137                         throw invalid_operation("Sequence::render");
138
139                 const FrameFormat &format = target_fbo->get_format();
140                 ClearValue clear_values[7];
141                 unsigned i = 0;
142                 Color default_color = (clear_colors.empty() ? Color(0.0f, 0.0f, 0.0f, 0.0f) : clear_colors.front());
143                 ClearValue *cv = clear_values;
144                 for(FrameAttachment a: format)
145                 {
146                         if(get_attach_point(a)==get_attach_point(DEPTH_ATTACHMENT))
147                                 cv->depth_stencil.depth = clear_depth;
148                         else if(get_attach_point(a)==get_attach_point(STENCIL_ATTACHMENT))
149                                 cv->depth_stencil.stencil = clear_stencil;
150                         else
151                                 cv->color = (i<clear_colors.size() ? clear_colors[i++] : default_color);
152                         ++cv;
153                 }
154
155                 renderer.clear(clear_values);
156         }
157
158         for(const Step &s: steps)
159         {
160                 Renderer::Push _push2(renderer);
161
162                 renderer.set_depth_test(&s.get_depth_test());
163                 renderer.set_stencil_test(&s.get_stencil_test());
164                 renderer.set_blend(&s.get_blend());
165
166                 if (const Lighting *lighting = s.get_lighting())
167                         renderer.add_shader_data(lighting->get_shader_data());
168                 renderer.set_clipping(s.get_clipping());
169
170                 if(const Renderable *renderable = s.get_renderable())
171                         renderer.render(*renderable, s.get_tag());
172         }
173
174         if(target[0])
175         {
176                 if(target_ms)
177                         renderer.resolve_multisample(target[0]->get_framebuffer(), COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
178
179                 renderer.set_depth_test(0);
180                 renderer.set_stencil_test(0);
181                 renderer.set_blend(0);
182
183                 for(unsigned i=0; i<postproc.size(); ++i)
184                 {
185                         unsigned j = i%2;
186                         renderer.set_framebuffer(i+1<postproc.size() ? &target[1-j]->get_framebuffer() : out_fbo);
187                         const Texture2D &color = target[j]->get_target_texture(COLOR_ATTACHMENT);
188                         const Texture2D &depth = target[j]->get_target_texture(DEPTH_ATTACHMENT);
189                         postproc[i].postproc->render(renderer, color, depth);
190                 }
191         }
192 }
193
194 void Sequence::set_debug_name(const string &name)
195 {
196 #ifdef DEBUG
197         for(unsigned i=0; i<2; ++i)
198                 if(target[i])
199                         target[i]->set_debug_name(format("%s [RT:%d]", name, i));
200         if(target_ms)
201                 target_ms->set_debug_name(name+" [RT:ms]");
202 #else
203         (void)name;
204 #endif
205 }
206
207
208 Sequence::Step::Step(Tag t, Renderable *r):
209         tag(t),
210         lighting(0),
211         clipping(0),
212         renderable(r)
213 { }
214
215 void Sequence::Step::set_lighting(const Lighting *l)
216 {
217         lighting = l;
218 }
219
220 void Sequence::Step::set_depth_test(const DepthTest &dt)
221 {
222         depth_test = dt;
223 }
224
225 void Sequence::Step::set_stencil_test(const StencilTest &st)
226 {
227         stencil_test = st;
228 }
229
230 void Sequence::Step::set_blend(const Blend &b)
231 {
232         blend = b;
233 }
234
235 void Sequence::Step::set_clipping(const Clipping *c)
236 {
237         clipping = c;
238 }
239
240 } // namespace GL
241 } // namespace Msp