]> git.tdb.fi Git - libs/gl.git/blob - source/builders/sequencetemplate.h
Refactor the structure of sequence template files
[libs/gl.git] / source / builders / sequencetemplate.h
1 #ifndef SEQUENCETEMPLATE_H_
2 #define SEQUENCETEMPLATE_H_
3
4 #include <string>
5 #include <vector>
6 #include <msp/core/typeregistry.h>
7 #include <msp/datafile/dynamicobjectloader.h>
8 #include <msp/datafile/objectloader.h>
9 #include "depthtest.h"
10 #include "postprocessor.h"
11 #include "resources.h"
12 #include "stenciltest.h"
13
14 namespace Msp {
15 namespace GL {
16
17 class Lighting;
18 class Renderable;
19
20 class SequenceTemplate
21 {
22 private:
23         template<typename T>
24         class TemplateLoader: public DataFile::DynamicObjectLoader<typename T::Template>
25         {
26                 friend class SequenceTemplate;
27
28         public:
29                 TemplateLoader(typename DataFile::DynamicObjectLoader<typename T::Template>::Collection &c): DataFile::DynamicObjectLoader<typename T::Template>(&c) { }
30
31         private:
32                 virtual typename DataFile::DynamicObjectLoader<typename T::Template>::TypeRegistry &get_type_registry() const { return get_registry<T>(); }
33         };
34
35         template<typename T>
36         using TemplateRegistry = typename TemplateLoader<T>::TypeRegistry;
37
38 public:
39         class Loader: public DataFile::CollectionObjectLoader<SequenceTemplate>
40         {
41         public:
42                 Loader(SequenceTemplate &, Collection &);
43
44         private:
45                 void clear();
46                 void multisample(unsigned);
47                 void multisample_range(unsigned, unsigned);
48                 void postprocessor();
49                 void postprocessor_with_slot(const std::string &);
50                 void renderable(const std::string &);
51                 void renderable_with_default(const std::string &, const std::string &);
52                 void step(const std::string &, const std::string &);
53         };
54
55         struct Renderable
56         {
57                 GL::Renderable *renderable = 0;
58                 std::string slot_name;
59         };
60
61         struct Step
62         {
63                 class Loader: public DataFile::CollectionObjectLoader<Step>
64                 {
65                 private:
66                         std::string inline_base_name;
67
68                 public:
69                         Loader(Step &, Collection &);
70
71                         void set_inline_base_name(const std::string &);
72
73                 private:
74                         void depth_test();
75                         void depth_compare(Predicate);
76                         void lighting(const std::string &);
77                         void lighting_inline();
78                         void stencil_test();
79                 };
80
81                 std::string tag;
82                 const Lighting *lighting = 0;
83                 DepthTest depth_test;
84                 StencilTest stencil_test;
85                 std::string renderable_name;
86         };
87
88         struct PostProcessor
89         {
90                 GL::PostProcessor::Template *postprocessor_template;
91                 std::string slot_name;
92
93                 PostProcessor(GL::PostProcessor::Template * = 0);
94         };
95
96 private:
97         class ClearLoader: public DataFile::ObjectLoader<SequenceTemplate>
98         {
99         public:
100                 ClearLoader(SequenceTemplate &);
101
102         private:
103                 void color(float, float, float, float);
104                 void depth(float);
105                 void stencil(int);
106         };
107
108         bool hdr = false;
109         bool alpha = false;
110         unsigned required_multisample = 0;
111         unsigned max_multisample = 0;
112         std::vector<Renderable> renderables;
113         std::vector<Step> steps;
114         std::vector<PostProcessor> postprocessors;
115         bool clear_enabled = false;
116         std::vector<Color> clear_colors;
117         float clear_depth = 1.0f;
118         int clear_stencil = 0;
119
120 public:
121         ~SequenceTemplate();
122
123         bool get_hdr() const { return hdr; }
124         bool get_alpha() const { return alpha; }
125         unsigned get_required_multisample() const { return required_multisample; }
126         unsigned get_maximum_multisample() const { return max_multisample; }
127         const std::vector<Renderable> &get_renderables() const { return renderables; }
128         const std::vector<Step> &get_steps() const { return steps; }
129         const std::vector<PostProcessor> &get_postprocessors() const { return postprocessors; }
130         bool is_clear_enabled() const { return clear_enabled; }
131         const std::vector<Color> &get_clear_colors() const { return clear_colors; }
132         float get_clear_depth() const { return clear_depth; }
133         int get_clear_stencil() const { return clear_stencil; }
134
135         template<typename T>
136         static void register_postprocessor(const std::string &);
137
138 private:
139         template<typename T>
140         static TemplateRegistry<T> &get_registry();
141 };
142
143 template<typename T>
144 void SequenceTemplate::register_postprocessor(const std::string &kw)
145 {
146         get_registry<GL::PostProcessor>().register_type<typename T::Template>(kw);
147 }
148
149 } // namespace GL
150 } // namespace Msp
151
152 #endif