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