]> git.tdb.fi Git - libs/gl.git/blob - source/pipelinebuilder.cpp
Support named postprocessor slots in pipeline templates
[libs/gl.git] / source / pipelinebuilder.cpp
1 #include <msp/core/algorithm.h>
2 #include <msp/core/maputils.h>
3 #include "error.h"
4 #include "pipeline.h"
5 #include "pipelinebuilder.h"
6 #include "pipelinetemplate.h"
7 #include "renderbuffer.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13
14 PipelineBuilder::PipelineBuilder(const PipelineTemplate &t):
15         tmpl(t)
16 {
17         const vector<PipelineTemplate::Pass> &passes = tmpl.get_passes();
18         for(vector<PipelineTemplate::Pass>::const_iterator i=passes.begin(); i!=passes.end(); ++i)
19                 renderables[i->renderable_name] = 0;
20         const vector<PipelineTemplate::PostProcessor> &postprocs = tmpl.get_postprocessors();
21         for(PipelineTemplate::PostProcessorArray::const_iterator i=postprocs.begin(); i!=postprocs.end(); ++i)
22                 if(!i->slot_name.empty())
23                         postprocessors[i->slot_name] = 0;
24 }
25
26 void PipelineBuilder::set_renderable(const string &name, Renderable &rend)
27 {
28         get_item(renderables, name) = &rend;
29 }
30
31 void PipelineBuilder::set_postprocessor(const string &name, PostProcessor &pproc)
32 {
33         get_item(postprocessors, name) = &pproc;
34 }
35
36 void PipelineBuilder::build(Pipeline &pipeline) const
37 {
38         pipeline.set_hdr(tmpl.get_hdr());
39         unsigned samples = min(tmpl.get_maximum_multisample(), Renderbuffer::get_max_samples());
40         if(samples<tmpl.get_required_multisample())
41                 throw invalid_operation("PipelineBuilder::build");
42
43         pipeline.set_multisample(samples);
44
45         const PipelineTemplate::PassArray &passes = tmpl.get_passes();
46         for(PipelineTemplate::PassArray::const_iterator i=passes.begin(); i!=passes.end(); ++i)
47         {
48                 Renderable *renderable = get_item(renderables, i->renderable_name);
49                 if(!renderable)
50                         continue;
51
52                 Pipeline::Pass &pass = pipeline.add_pass(i->tag, *renderable);
53                 pass.set_blend(i->blend.get());
54                 pass.set_depth_test(i->depth_test.get());
55                 pass.set_lighting(i->lighting.get());
56         }
57
58         const PipelineTemplate::PostProcessorArray &postprocs = tmpl.get_postprocessors();
59         for(PipelineTemplate::PostProcessorArray::const_iterator i=postprocs.begin(); i!=postprocs.end(); ++i)
60         {
61                 PostProcessor *proc = 0;
62                 if(!i->slot_name.empty())
63                         proc = get_item(postprocessors, i->slot_name);
64                 if(!proc && i->postprocessor_template)
65                         proc = i->postprocessor_template->create(pipeline.get_width(), pipeline.get_height());
66                 if(proc)
67                         pipeline.add_postprocessor_owned(proc);
68         }
69 }
70
71 Pipeline *PipelineBuilder::build(unsigned w, unsigned h) const
72 {
73         RefPtr<Pipeline> pipeline = new Pipeline(w, h);
74         build(*pipeline);
75         return pipeline.release();
76 }
77
78 Pipeline *PipelineBuilder::build(const View &view) const
79 {
80         RefPtr<Pipeline> pipeline = new Pipeline(view);
81         build(*pipeline);
82         return pipeline.release();
83 }
84
85 Pipeline *PipelineBuilder::build(const Framebuffer &fbo) const
86 {
87         RefPtr<Pipeline> pipeline = new Pipeline(fbo);
88         build(*pipeline);
89         return pipeline.release();
90 }
91
92 } // namespace GL
93 } // namespace Msp