]> git.tdb.fi Git - libs/gl.git/blob - source/builders/sequencetemplate.cpp
Replace some local pointers with references in VulkanCommands
[libs/gl.git] / source / builders / sequencetemplate.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/datafile/collection.h>
3 #include <msp/strings/format.h>
4 #include "ambientocclusion.h"
5 #include "blend.h"
6 #include "bloom.h"
7 #include "colorcurve.h"
8 #include "environmentmap.h"
9 #include "lighting.h"
10 #include "object.h"
11 #include "resources.h"
12 #include "scene.h"
13 #include "sequencetemplate.h"
14 #include "shadowmap.h"
15 #include "sky.h"
16
17 using namespace std;
18
19 namespace Msp {
20 namespace GL {
21
22 SequenceTemplate::~SequenceTemplate()
23 {
24         for(const Renderable &r: renderables)
25                 delete r.effect_template;
26         for(const PostProcessor &p: postprocessors)
27                 delete p.postprocessor_template;
28 }
29
30 template<>
31 SequenceTemplate::TemplateRegistry<GL::PostProcessor> &SequenceTemplate::get_registry<GL::PostProcessor>()
32 {
33         static TemplateRegistry<GL::PostProcessor> registry;
34         static bool initialized = false;
35         if(!initialized)
36         {
37                 registry.register_type<AmbientOcclusion::Template>("ambient_occlusion");
38                 registry.register_type<Bloom::Template>("bloom");
39                 registry.register_type<ColorCurve::Template>("colorcurve");
40                 initialized = true;
41         }
42         return registry;
43 }
44
45 template<>
46 SequenceTemplate::TemplateRegistry<Effect> &SequenceTemplate::get_registry<Effect>()
47 {
48         static TemplateRegistry<Effect> registry;
49         static bool initialized = false;
50         if(!initialized)
51         {
52                 registry.register_type<EnvironmentMap::Template>("environment_map");
53                 registry.register_type<ShadowMap::Template>("shadow_map");
54                 registry.register_type<Sky::Template>("sky");
55                 initialized = true;
56         }
57         return registry;
58 }
59
60
61 SequenceTemplate::PostProcessor::PostProcessor(GL::PostProcessor::Template *ppt):
62         postprocessor_template(ppt)
63 { }
64
65
66 DataFile::Loader::ActionMap SequenceTemplate::Loader::shared_actions;
67
68 SequenceTemplate::Loader::Loader(SequenceTemplate &t, Collection &c):
69         CollectionObjectLoader<SequenceTemplate>(t, &c)
70 {
71         set_actions(shared_actions);
72 }
73
74 void SequenceTemplate::Loader::init_actions()
75 {
76         add("hdr", &SequenceTemplate::hdr);
77         add("clear", &Loader::clear);
78         add("effect", &Loader::effect);
79         add("multisample", &Loader::multisample);
80         add("multisample", &Loader::multisample_range);
81         add("postprocessor", &Loader::postprocessor);
82         add("postprocessor", &Loader::postprocessor_with_slot);
83         add("renderable", &Loader::renderable);
84         add("renderable", &Loader::renderable_with_default);
85         add("sequence", &Loader::sequence);
86         add("step", &Loader::step);
87 }
88
89 void SequenceTemplate::Loader::clear()
90 {
91         ClearLoader ldr(obj);
92         load_sub_with(ldr);
93         obj.clear_enabled = true;
94 }
95
96 void SequenceTemplate::Loader::effect(const string &slot)
97 {
98         TemplateLoader<Effect> ldr(get_collection());
99         load_sub_with(ldr);
100         Renderable rend;
101         rend.slot_name = slot;
102         rend.effect_template = ldr.get_object();
103         obj.renderables.push_back(rend);
104 }
105
106 void SequenceTemplate::Loader::multisample(unsigned samples)
107 {
108         obj.required_multisample = samples;
109         obj.max_multisample = samples;
110 }
111
112 void SequenceTemplate::Loader::multisample_range(unsigned req, unsigned max)
113 {
114         obj.required_multisample = req;
115         obj.max_multisample = max;
116 }
117
118 void SequenceTemplate::Loader::postprocessor()
119 {
120         postprocessor_with_slot(string());
121 }
122
123 void SequenceTemplate::Loader::postprocessor_with_slot(const string &slot)
124 {
125         TemplateLoader<GL::PostProcessor> ldr(get_collection());
126         load_sub_with(ldr);
127         PostProcessor pp;
128         pp.postprocessor_template = ldr.get_object();
129         pp.slot_name = slot;
130         obj.postprocessors.push_back(pp);
131 }
132
133 void SequenceTemplate::Loader::renderable(const string &slot)
134 {
135         Renderable rend;
136         rend.slot_name = slot;
137         obj.renderables.push_back(rend);
138 }
139
140 void SequenceTemplate::Loader::renderable_with_default(const string &slot, const string &name)
141 {
142         Renderable rend;
143         rend.renderable = &get_collection().get<GL::Renderable>(name);
144         rend.slot_name = slot;
145         obj.renderables.push_back(rend);
146 }
147
148 void SequenceTemplate::Loader::sequence(const string &slot, const string &name)
149 {
150         Renderable rend;
151         rend.sequence_template = &get_collection().get<SequenceTemplate>(name);
152         rend.slot_name = slot;
153         SequenceLoader ldr(rend);
154         load_sub_with(ldr);
155         obj.renderables.push_back(rend);
156 }
157
158 void SequenceTemplate::Loader::step(const string &tag, const string &rend)
159 {
160         Step stp;
161         stp.tag = tag;
162         stp.renderable_name = rend;
163         Step::Loader ldr(stp, get_collection());
164         ldr.set_inline_base_name(format("%s/%d.step", get_source(), obj.steps.size()));
165         load_sub_with(ldr);
166
167         obj.steps.push_back(stp);
168 }
169
170
171 DataFile::Loader::ActionMap SequenceTemplate::ClearLoader::shared_actions;
172
173 SequenceTemplate::ClearLoader::ClearLoader(SequenceTemplate &t):
174         ObjectLoader<SequenceTemplate>(t)
175 {
176         set_actions(shared_actions);
177 }
178
179 void SequenceTemplate::ClearLoader::init_actions()
180 {
181         add("color", &ClearLoader::color);
182         add("depth", &ClearLoader::depth);
183         add("stencil", &ClearLoader::stencil);
184 }
185
186 void SequenceTemplate::ClearLoader::color(float r, float g, float b, float a)
187 {
188         obj.clear_colors.emplace_back(r, g, b, a);
189 }
190
191 void SequenceTemplate::ClearLoader::depth(float d)
192 {
193         obj.clear_depth = d;
194 }
195
196 void SequenceTemplate::ClearLoader::stencil(int s)
197 {
198         obj.clear_stencil = s;
199 }
200
201
202 DataFile::Loader::ActionMap SequenceTemplate::SequenceLoader::shared_actions;
203
204 SequenceTemplate::SequenceLoader::SequenceLoader(Renderable &r):
205         ObjectLoader<Renderable>(r)
206 {
207         set_actions(shared_actions);
208 }
209
210 void SequenceTemplate::SequenceLoader::init_actions()
211 {
212         add("renderable", &SequenceLoader::renderable);
213 }
214
215 void SequenceTemplate::SequenceLoader::renderable(const string &slot, const string &name)
216 {
217         obj.sequence_renderables[slot] = name;
218 }
219
220
221 DataFile::Loader::ActionMap SequenceTemplate::Step::Loader::shared_actions;
222
223 SequenceTemplate::Step::Loader::Loader(Step &p, Collection &c):
224         DataFile::CollectionObjectLoader<Step>(p, &c)
225 {
226         set_actions(shared_actions);
227 }
228
229 void SequenceTemplate::Step::Loader::init_actions()
230 {
231         add("depth_test", &Loader::depth_test);
232         add("depth_test", &Loader::depth_compare);
233         add("lighting", &Loader::lighting);
234         add("lighting", &Loader::lighting_inline);
235         add("stencil_test", &Loader::stencil_test);
236 }
237
238 void SequenceTemplate::Step::Loader::set_inline_base_name(const string &n)
239 {
240         inline_base_name = n;
241 }
242
243 void SequenceTemplate::Step::Loader::depth_test()
244 {
245         load_sub(obj.depth_test);
246 }
247
248 void SequenceTemplate::Step::Loader::depth_compare(Predicate c)
249 {
250         obj.depth_test = DepthTest(c);
251 }
252
253 void SequenceTemplate::Step::Loader::lighting_inline()
254 {
255         RefPtr<Lighting> lightn = new Lighting;
256         load_sub(*lightn, get_collection());
257         get_collection().add(inline_base_name+".lightn", lightn.get());
258         obj.lighting = lightn.release();
259 }
260
261 void SequenceTemplate::Step::Loader::lighting(const string &name)
262 {
263         obj.lighting = &get_collection().get<Lighting>(name);
264 }
265
266 void SequenceTemplate::Step::Loader::stencil_test()
267 {
268         load_sub(obj.stencil_test);
269 }
270
271 } // namespace GL
272 } // namespace Msp