]> git.tdb.fi Git - libs/gl.git/blobdiff - source/builders/sequencebuilder.cpp
Rearrange postprocessor creation code in SequenceBuilder
[libs/gl.git] / source / builders / sequencebuilder.cpp
index 9bb8f1dcd6b05def0558b6f81cfa7767dd5061e7..f007350eeaa0e76ad023055f59d133bd407e368a 100644 (file)
@@ -3,10 +3,10 @@
 #include <msp/strings/format.h>
 #include "deviceinfo.h"
 #include "error.h"
-#include "renderbuffer.h"
 #include "sequence.h"
 #include "sequencebuilder.h"
 #include "sequencetemplate.h"
+#include "view.h"
 
 using namespace std;
 
@@ -16,13 +16,11 @@ namespace GL {
 SequenceBuilder::SequenceBuilder(const SequenceTemplate &t):
        tmpl(t)
 {
-       const vector<SequenceTemplate::Step> &steps = tmpl.get_steps();
-       for(vector<SequenceTemplate::Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
-               renderables[i->slot_name] = i->default_renderable;
-       const vector<SequenceTemplate::PostProcessor> &postprocs = tmpl.get_postprocessors();
-       for(SequenceTemplate::PostProcessorArray::const_iterator i=postprocs.begin(); i!=postprocs.end(); ++i)
-               if(!i->slot_name.empty())
-                       postprocessors[i->slot_name] = 0;
+       for(const SequenceTemplate::Renderable &r: tmpl.get_renderables())
+               renderables[r.slot_name] = r.renderable;
+       for(const SequenceTemplate::PostProcessor &p: tmpl.get_postprocessors())
+               if(!p.slot_name.empty())
+                       postprocessors[p.slot_name] = 0;
 }
 
 void SequenceBuilder::set_renderable(const string &name, Renderable &rend)
@@ -51,70 +49,94 @@ void SequenceBuilder::build(Sequence &sequence) const
                sequence.set_debug_name(debug_name);
 #endif
 
-       sequence.set_hdr(tmpl.get_hdr());
-       sequence.set_alpha(tmpl.get_alpha());
-       unsigned samples = min(tmpl.get_maximum_multisample(), Limits::get_global().max_samples);
-       if(samples<tmpl.get_required_multisample())
-               throw invalid_operation("SequenceBuilder::build");
-
-       sequence.set_multisample(samples);
+       sequence.set_clear_enabled(tmpl.is_clear_enabled());
+       if(tmpl.is_clear_enabled())
+       {
+               sequence.set_clear_colors(tmpl.get_clear_colors());
+               sequence.set_clear_depth(tmpl.get_clear_depth());
+               sequence.set_clear_stencil(tmpl.get_clear_stencil());
+       }
 
-       const vector<SequenceTemplate::Step> &steps = tmpl.get_steps();
-       for(vector<SequenceTemplate::Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
+       for(const SequenceTemplate::Step &s: tmpl.get_steps())
        {
-               Renderable *renderable = get_item(renderables, i->slot_name);
+               Renderable *renderable = get_item(renderables, s.renderable_name);
                if(!renderable)
-                       continue;
+                       throw missing_renderable(s.renderable_name);
 
-               Sequence::Step &step = sequence.add_step(i->tag, *renderable);
-               step.set_blend(i->blend.get());
-               step.set_depth_test(i->depth_test.get());
-               step.set_lighting(i->lighting);
+               Sequence::Step &step = sequence.add_step(s.tag, *renderable);
+               step.set_depth_test(s.depth_test);
+               step.set_stencil_test(s.stencil_test);
+               step.set_lighting(s.lighting);
        }
 
-       const SequenceTemplate::PostProcessorArray &postprocs = tmpl.get_postprocessors();
-       for(SequenceTemplate::PostProcessorArray::const_iterator i=postprocs.begin(); i!=postprocs.end(); ++i)
+#ifdef DEBUG
+       unsigned index = 0;
+#endif
+       for(const SequenceTemplate::PostProcessor &p: tmpl.get_postprocessors())
        {
                PostProcessor *proc = 0;
-               if(!i->slot_name.empty())
-                       proc = get_item(postprocessors, i->slot_name);
-               if(proc)
-                       sequence.add_postprocessor(*proc);
-               else if(i->postprocessor_template)
+               if(!p.slot_name.empty())
+                       proc = get_item(postprocessors, p.slot_name);
+               if(!proc)
                {
-                       proc = i->postprocessor_template->create(sequence.get_width(), sequence.get_height());
-                       if(proc)
+                       if(p.postprocessor_template)
                        {
+                               proc = p.postprocessor_template->create(sequence.get_width(), sequence.get_height());
 #ifdef DEBUG
                                if(!debug_name.empty())
-                                       proc->set_debug_name(format("%s/%d.pproc", debug_name, i-postprocs.begin()));
+                                       proc->set_debug_name(format("%s/%d.pproc", debug_name, index++));
 #endif
-                               sequence.add_postprocessor_owned(proc);
                        }
+                       else
+                               throw invalid_operation("SequenceBuilder::build");
+
+                       sequence.add_owned(proc);
                }
+
+               sequence.add_postprocessor(*proc);
        }
 }
 
+Sequence *SequenceBuilder::build() const
+{
+       RefPtr<Sequence> sequence = new Sequence();
+       build(*sequence);
+       return sequence.release();
+}
+
 Sequence *SequenceBuilder::build(unsigned w, unsigned h) const
 {
-       RefPtr<Sequence> sequence = new Sequence(w, h);
+       RefPtr<Sequence> sequence = new Sequence(w, h, create_frame_format());
        build(*sequence);
        return sequence.release();
 }
 
 Sequence *SequenceBuilder::build(const View &view) const
 {
-       RefPtr<Sequence> sequence = new Sequence(view);
+       RefPtr<Sequence> sequence = new Sequence(view.get_width(), view.get_height(), create_frame_format());
        build(*sequence);
        return sequence.release();
 }
 
 Sequence *SequenceBuilder::build(const Framebuffer &fbo) const
 {
-       RefPtr<Sequence> sequence = new Sequence(fbo);
+       RefPtr<Sequence> sequence = new Sequence(fbo.get_width(), fbo.get_height(), create_frame_format());
        build(*sequence);
        return sequence.release();
 }
 
+FrameFormat SequenceBuilder::create_frame_format() const
+{
+       unsigned samples = min(tmpl.get_maximum_multisample(), DeviceInfo::get_global().limits.max_samples);
+       if(samples<tmpl.get_required_multisample())
+               throw invalid_operation("SequenceBuilder::create_frame_format");
+
+       PixelComponents color_comp = (tmpl.get_alpha() ? RGBA : RGB);
+       DataType color_type = (tmpl.get_hdr() ? HALF_FLOAT : UNSIGNED_BYTE);
+       PixelFormat color_pf = make_pixelformat(color_comp, color_type);
+
+       return (COLOR_ATTACHMENT,color_pf, DEPTH_ATTACHMENT).set_samples(samples);
+}
+
 } // namespace GL
 } // namespace Msp