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(OwnedObject &o: owned_data)
37 o.delete_func(o.pointer);
43 void Sequence::set_clear_enabled(bool c)
48 void Sequence::set_clear_colors(const vector<Color> &c)
54 void Sequence::set_clear_depth(float d)
60 void Sequence::set_clear_stencil(int s)
66 Sequence::Step &Sequence::add_step(Tag tag, Renderable &r)
68 steps.emplace_back(tag, &r);
72 void Sequence::add_postprocessor(PostProcessor &pp)
74 if(target_format.empty())
75 throw invalid_operation("Sequence::add_postprocessor");
76 postproc.push_back(&pp);
79 void Sequence::setup_frame(Renderer &renderer)
81 for(const Step &s: steps)
82 if(Renderable *renderable = s.get_renderable())
83 renderable->setup_frame(renderer);
86 void Sequence::finish_frame()
88 for(const Step &s: steps)
89 if(Renderable *renderable = s.get_renderable())
90 renderable->finish_frame();
93 void Sequence::render(Renderer &renderer, Tag tag) const
95 if(tag.id && tag!=noclear_tag)
98 Renderer::Push _push(renderer);
100 const Framebuffer *out_fbo = renderer.get_framebuffer();
103 renderer.set_framebuffer(&(target_ms ? target_ms : target[0])->get_framebuffer());
105 if(clear_enabled && tag!=noclear_tag)
107 const Framebuffer *target_fbo = renderer.get_framebuffer();
109 throw invalid_operation("Sequence::render");
111 const FrameFormat &format = target_fbo->get_format();
112 ClearValue clear_values[FrameFormat::MAX_ATTACHMENTS];
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)
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;
123 cv->color = (i<clear_colors.size() ? clear_colors[i++] : default_color);
127 renderer.clear(clear_values);
130 for(const Step &s: steps)
132 Renderer::Push _push2(renderer);
134 renderer.set_depth_test(&s.get_depth_test());
135 renderer.set_stencil_test(&s.get_stencil_test());
137 if(const Lighting *lighting = s.get_lighting())
138 renderer.add_shader_data(lighting->get_shader_data());
140 if(const Renderable *renderable = s.get_renderable())
141 renderable->render(renderer, s.get_tag());
147 renderer.resolve_multisample(target[0]->get_framebuffer());
149 renderer.set_depth_test(0);
150 renderer.set_stencil_test(0);
151 renderer.set_blend(0);
153 for(unsigned i=0; i<postproc.size(); ++i)
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);
164 void Sequence::set_debug_name(const string &name)
167 for(unsigned i=0; i<2; ++i)
169 target[i]->set_debug_name(format("%s [RT:%d]", name, i));
171 target_ms->set_debug_name(name+" [RT:ms]");
178 Sequence::Step::Step(Tag t, Renderable *r):
184 void Sequence::Step::set_lighting(const Lighting *l)
189 void Sequence::Step::set_depth_test(const DepthTest &dt)
194 void Sequence::Step::set_stencil_test(const StencilTest &st)