]> git.tdb.fi Git - libs/gl.git/blob - source/render/sequence.cpp
Use the changes member directly in PipelineState backends
[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(OwnedObject &o: owned_data)
37                 o.delete_func(o.pointer);
38         delete target[0];
39         delete target[1];
40         delete target_ms;
41 }
42
43 void Sequence::set_clear_enabled(bool c)
44 {
45         clear_enabled = c;
46 }
47
48 void Sequence::set_clear_colors(const vector<Color> &c)
49 {
50         clear_enabled = true;
51         clear_colors = c;
52 }
53
54 void Sequence::set_clear_depth(float d)
55 {
56         clear_enabled = true;
57         clear_depth = d;
58 }
59
60 void Sequence::set_clear_stencil(int s)
61 {
62         clear_enabled = true;
63         clear_stencil = s;
64 }
65
66 Sequence::Step &Sequence::add_step(Tag tag, Renderable &r)
67 {
68         steps.emplace_back(tag, &r);
69         return steps.back();
70 }
71
72 void Sequence::add_postprocessor(PostProcessor &pp)
73 {
74         if(target_format.empty())
75                 throw invalid_operation("Sequence::add_postprocessor");
76         postproc.push_back(&pp);
77 }
78
79 void Sequence::setup_frame(Renderer &renderer)
80 {
81         for(const Step &s: steps)
82                 if(Renderable *renderable = s.get_renderable())
83                         renderable->setup_frame(renderer);
84 }
85
86 void Sequence::finish_frame()
87 {
88         for(const Step &s: steps)
89                 if(Renderable *renderable = s.get_renderable())
90                         renderable->finish_frame();
91 }
92
93 void Sequence::render(Renderer &renderer, Tag tag) const
94 {
95         if(tag.id && tag!=noclear_tag)
96                 return;
97
98         Renderer::Push _push(renderer);
99
100         const Framebuffer *out_fbo = renderer.get_framebuffer();
101
102         if(target[0])
103                 renderer.set_framebuffer(&(target_ms ? target_ms : target[0])->get_framebuffer());
104
105         if(clear_enabled && tag!=noclear_tag)
106         {
107                 const Framebuffer *target_fbo = renderer.get_framebuffer();
108                 if(!target_fbo)
109                         throw invalid_operation("Sequence::render");
110
111                 const FrameFormat &format = target_fbo->get_format();
112                 ClearValue clear_values[FrameFormat::MAX_ATTACHMENTS];
113                 unsigned i = 0;
114                 Color default_color = (clear_colors.empty() ? Color(0.0f, 0.0f, 0.0f, 0.0f) : clear_colors.front());
115                 ClearValue *cv = clear_values;
116                 for(FrameAttachment a: format)
117                 {
118                         if(get_attach_point(a)==get_attach_point(DEPTH_ATTACHMENT))
119                                 cv->depth_stencil.depth = clear_depth;
120                         else if(get_attach_point(a)==get_attach_point(STENCIL_ATTACHMENT))
121                                 cv->depth_stencil.stencil = clear_stencil;
122                         else
123                                 cv->color = (i<clear_colors.size() ? clear_colors[i++] : default_color);
124                         ++cv;
125                 }
126
127                 renderer.clear(clear_values);
128         }
129
130         for(const Step &s: steps)
131         {
132                 Renderer::Push _push2(renderer);
133
134                 renderer.set_depth_test(&s.get_depth_test());
135                 renderer.set_stencil_test(&s.get_stencil_test());
136
137                 if(const Lighting *lighting = s.get_lighting())
138                         renderer.add_shader_data(lighting->get_shader_data());
139
140                 if(const Renderable *renderable = s.get_renderable())
141                         renderable->render(renderer, s.get_tag());
142         }
143
144         if(target[0])
145         {
146                 if(target_ms)
147                         renderer.resolve_multisample(target[0]->get_framebuffer());
148
149                 renderer.set_depth_test(0);
150                 renderer.set_stencil_test(0);
151                 renderer.set_blend(0);
152
153                 for(unsigned i=0; i<postproc.size(); ++i)
154                 {
155                         unsigned j = i%2;
156                         renderer.set_framebuffer(i+1<postproc.size() ? &target[1-j]->get_framebuffer() : out_fbo);
157                         const Texture2D &color = target[j]->get_target_texture(COLOR_ATTACHMENT);
158                         const Texture2D &depth = target[j]->get_target_texture(DEPTH_ATTACHMENT);
159                         postproc[i]->render(renderer, color, depth);
160                 }
161         }
162 }
163
164 void Sequence::set_debug_name(const string &name)
165 {
166 #ifdef DEBUG
167         for(unsigned i=0; i<2; ++i)
168                 if(target[i])
169                         target[i]->set_debug_name(format("%s [RT:%d]", name, i));
170         if(target_ms)
171                 target_ms->set_debug_name(name+" [RT:ms]");
172 #else
173         (void)name;
174 #endif
175 }
176
177
178 Sequence::Step::Step(Tag t, Renderable *r):
179         tag(t),
180         lighting(0),
181         renderable(r)
182 { }
183
184 void Sequence::Step::set_lighting(const Lighting *l)
185 {
186         lighting = l;
187 }
188
189 void Sequence::Step::set_depth_test(const DepthTest &dt)
190 {
191         depth_test = dt;
192 }
193
194 void Sequence::Step::set_stencil_test(const StencilTest &st)
195 {
196         stencil_test = st;
197 }
198
199 } // namespace GL
200 } // namespace Msp