Migrate from LoadableTypeRegistry to TypeRegistry
[libs/gl.git] / source / builders / pipelinetemplate.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/datafile/collection.h>
3 #include "ambientocclusion.h"
4 #include "blend.h"
5 #include "bloom.h"
6 #include "colorcurve.h"
7 #include "lighting.h"
8 #include "pipelinetemplate.h"
9 #include "resources.h"
10 #include "tests.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace GL {
16
17 PipelineTemplate::PipelineTemplate():
18         resources(0),
19         hdr(false),
20         alpha(false),
21         required_multisample(0),
22         max_multisample(0)
23 { }
24
25 PipelineTemplate::~PipelineTemplate()
26 {
27         for(PostProcessorArray::iterator i=postprocessors.begin(); i!=postprocessors.end(); ++i)
28                 delete i->postprocessor_template;
29 }
30
31 Resources &PipelineTemplate::get_resources() const
32 {
33         if(!resources)  
34                 throw logic_error("no resources");
35         return *resources;
36 }
37
38
39 PipelineTemplate::PostProcessorRegistry &PipelineTemplate::get_postprocessor_registry()
40 {
41         static PostProcessorRegistry registry;
42         static bool initialized = false;
43         if(!initialized)
44         {
45                 registry.register_type<AmbientOcclusion>("ambient_occlusion");
46                 registry.register_type<Bloom>("bloom");
47                 registry.register_type<ColorCurve>("colorcurve");
48                 initialized = true;
49         }
50         return registry;
51 }
52
53
54 PipelineTemplate::Pass::~Pass()
55 { }
56
57
58 PipelineTemplate::PostProcessor::PostProcessor(GL::PostProcessor::Template *ppt):
59         postprocessor_template(ppt)
60 { }
61
62
63 PipelineTemplate::PostProcLoader::PostProcLoader()
64 {
65         get_postprocessor_registry().invoke_all(*this);
66 }
67
68
69 PipelineTemplate::Loader::Loader(PipelineTemplate &t, Collection &c):
70         DataFile::CollectionObjectLoader<PipelineTemplate, Resources>(t, &c)
71 {
72         add("hdr", &PipelineTemplate::hdr);
73         add("alpha", &PipelineTemplate::alpha);
74         add("multisample", &Loader::multisample);
75         add("multisample", &Loader::multisample_range);
76         add("pass", &Loader::pass);
77         add("postprocessor", &Loader::postprocessor);
78
79         obj.resources = &c;
80 }
81
82 void PipelineTemplate::Loader::postprocessor_loaded()
83 {
84         obj.postprocessors.push_back(get_postprocessor_template());
85 }
86
87 void PipelineTemplate::Loader::multisample(unsigned samples)
88 {
89         obj.required_multisample = samples;
90         obj.max_multisample = samples;
91 }
92
93 void PipelineTemplate::Loader::multisample_range(unsigned req, unsigned max)
94 {
95         obj.required_multisample = req;
96         obj.max_multisample = max;
97 }
98
99 void PipelineTemplate::Loader::pass(const string &tag, const string &rend)
100 {
101         Pass pss;;
102         pss.tag = tag;
103         pss.renderable_name = rend;
104         if(coll)
105                 load_sub(pss, *coll);
106         else
107                 load_sub(pss);
108
109         obj.passes.push_back(pss);
110 }
111
112 void PipelineTemplate::Loader::postprocessor(const string &slot)
113 {
114         PostProcLoader ldr;
115         load_sub_with(ldr);
116         PostProcessor pp;
117         pp.postprocessor_template = ldr.get_postprocessor_template();
118         pp.slot_name = slot;
119         obj.postprocessors.push_back(pp);
120 }
121
122
123 PipelineTemplate::Pass::Loader::Loader(Pass &p):
124         DataFile::CollectionObjectLoader<Pass>(p, 0)
125 {
126         init();
127 }
128
129 PipelineTemplate::Pass::Loader::Loader(Pass &p, Collection &c):
130         DataFile::CollectionObjectLoader<Pass>(p, &c)
131 {
132         init();
133 }
134
135 void PipelineTemplate::Pass::Loader::init()
136 {
137         add("blend", &Loader::blend);
138         add("blend", &Loader::blend_predefined);
139         add("depth_test", &Loader::depth_test);
140         add("depth_test", &Loader::depth_test_predefined);
141         add("lighting", &Loader::lighting);
142         add("lighting", &Loader::lighting_inline);
143 }
144
145 void PipelineTemplate::Pass::Loader::blend_predefined(const string &name)
146 {
147         const Blend *bln = 0;
148         if(name=="alpha")
149                 bln = &Blend::alpha();
150         else if(name=="additive")
151                 bln = &Blend::additive();
152         else if(name=="additive_alpha")
153                 bln = &Blend::additive_alpha();
154         else
155                 throw key_error(name);
156
157         obj.blend = bln;
158         obj.blend.keep();
159 }
160
161 void PipelineTemplate::Pass::Loader::blend(BlendFactor src, BlendFactor dest)
162 {
163         obj.blend = new Blend(src, dest);
164 }
165
166 void PipelineTemplate::Pass::Loader::depth_test_predefined(const string &name)
167 {
168         const DepthTest *dtest = 0;
169         if(name=="lequal")
170                 dtest = &DepthTest::lequal();
171         else
172                 throw key_error(name);
173
174         obj.depth_test = dtest;
175         obj.depth_test.keep();
176 }
177
178 void PipelineTemplate::Pass::Loader::depth_test(Predicate pred)
179 {
180         obj.depth_test = new DepthTest(pred);
181 }
182
183 void PipelineTemplate::Pass::Loader::lighting_inline()
184 {
185         RefPtr<Lighting> lightn = new Lighting;
186         load_sub(*lightn);
187         obj.lighting = lightn;
188 }
189
190 void PipelineTemplate::Pass::Loader::lighting(const string &name)
191 {
192         obj.lighting = &get_collection().get<Lighting>(name);
193         obj.lighting.keep();
194 }
195
196 /*void PipelineTemplate::Pass::Loader::scene(const string &name)
197 {
198         obj.default_renderable = get_collection().get<Scene>(name);
199 }*/
200
201 } // namespace GL
202 } // namespace Msp