1 #include <msp/strings/format.h>
4 #include "postprocessor.h"
6 #include "rendertarget.h"
15 Tag Sequence::noclear_tag = "noclear";
17 Sequence::Sequence(unsigned w, unsigned h, const FrameFormat &f):
22 if(target_format.empty())
23 throw invalid_argument("Sequence::Sequence");
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);
30 if(target_format.get_samples()>1)
31 target_ms = new RenderTarget(width, height, target_format);
36 for(PostProcStep &p: postproc)
44 void Sequence::set_clear_enabled(bool c)
49 void Sequence::set_clear_colors(const vector<Color> &c)
55 void Sequence::set_clear_depth(float d)
61 void Sequence::set_clear_stencil(int s)
67 Sequence::Step &Sequence::add_step(Tag tag, Renderable &r)
69 steps.push_back(Step(tag, &r));
73 void Sequence::add_postprocessor(PostProcessor &pp)
75 add_postprocessor(&pp, false);
78 void Sequence::add_postprocessor_owned(PostProcessor *pp)
80 add_postprocessor(pp, true);
83 void Sequence::add_postprocessor(PostProcessor *pp, bool owned)
85 if(target_format.empty())
89 throw invalid_operation("Sequence::add_postprocessor");
91 postproc.push_back(PostProcStep(pp, owned));
94 void Sequence::setup_frame(Renderer &renderer)
96 for(const Step &s: steps)
97 if(Renderable *renderable = s.get_renderable())
98 renderable->setup_frame(renderer);
101 void Sequence::finish_frame()
103 for(const Step &s: steps)
104 if(Renderable *renderable = s.get_renderable())
105 renderable->finish_frame();
108 void Sequence::render(Renderer &renderer, Tag tag) const
110 if(tag.id && tag!=noclear_tag)
113 Renderer::Push _push(renderer);
115 const Framebuffer *out_fbo = renderer.get_framebuffer();
118 renderer.set_framebuffer(&(target_ms ? target_ms : target[0])->get_framebuffer());
120 if(clear_enabled && tag!=noclear_tag)
122 const Framebuffer *target_fbo = renderer.get_framebuffer();
124 throw invalid_operation("Sequence::render");
126 const FrameFormat &format = target_fbo->get_format();
127 ClearValue clear_values[7];
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)
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;
138 cv->color = (i<clear_colors.size() ? clear_colors[i++] : default_color);
142 renderer.clear(clear_values);
145 for(const Step &s: steps)
147 Renderer::Push _push2(renderer);
149 renderer.set_depth_test(&s.get_depth_test());
150 renderer.set_stencil_test(&s.get_stencil_test());
152 if(const Lighting *lighting = s.get_lighting())
153 renderer.add_shader_data(lighting->get_shader_data());
155 if(const Renderable *renderable = s.get_renderable())
156 renderer.render(*renderable, s.get_tag());
162 renderer.resolve_multisample(target[0]->get_framebuffer());
164 renderer.set_depth_test(0);
165 renderer.set_stencil_test(0);
166 renderer.set_blend(0);
168 for(unsigned i=0; i<postproc.size(); ++i)
171 renderer.set_framebuffer(i+1<postproc.size() ? &target[1-j]->get_framebuffer() : out_fbo);
172 const Texture2D &color = target[j]->get_target_texture(COLOR_ATTACHMENT);
173 const Texture2D &depth = target[j]->get_target_texture(DEPTH_ATTACHMENT);
174 postproc[i].postproc->render(renderer, color, depth);
179 void Sequence::set_debug_name(const string &name)
182 for(unsigned i=0; i<2; ++i)
184 target[i]->set_debug_name(format("%s [RT:%d]", name, i));
186 target_ms->set_debug_name(name+" [RT:ms]");
193 Sequence::Step::Step(Tag t, Renderable *r):
199 void Sequence::Step::set_lighting(const Lighting *l)
204 void Sequence::Step::set_depth_test(const DepthTest &dt)
209 void Sequence::Step::set_stencil_test(const StencilTest &st)