]> git.tdb.fi Git - libs/gl.git/blob - source/builders/sequencetemplate.h
660a043297bb2f8046c283bf6954ae538d620f09
[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/objectloader.h>
8 #include "blend.h"
9 #include "depthtest.h"
10 #include "postprocessor.h"
11 #include "predicate.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         class PostProcLoader: virtual public DataFile::Loader
24         {
25         private:
26                 template<typename T>
27                 struct AddPostProc
28                 {
29                         void operator()(const std::string &kw, PostProcLoader &ldr) const { ldr.add(kw, &PostProcLoader::postprocessor<T>); }
30                 };
31
32         protected:
33                 RefPtr<PostProcessor::Template> postproc;
34
35         public:
36                 PostProcLoader();
37
38                 PostProcessor::Template *get_postprocessor_template() { return postproc.release(); }
39
40         protected:
41                 virtual void postprocessor_loaded() { }
42
43         private:
44                 template<typename T>
45                 void postprocessor();
46
47                 friend class SequenceTemplate;
48         };
49
50 public:
51         class Loader: public DataFile::CollectionObjectLoader<SequenceTemplate, Resources>, public PostProcLoader
52         {
53         public:
54                 Loader(SequenceTemplate &, Collection &);
55
56                 virtual void postprocessor_loaded();
57                 void clear();
58                 void multisample(unsigned);
59                 void multisample_range(unsigned, unsigned);
60                 void postprocessor(const std::string &);
61                 void step(const std::string &);
62                 void step_with_slot(const std::string &, const std::string &);
63         };
64
65         struct Step
66         {
67                 class Loader: public DataFile::CollectionObjectLoader<Step>
68                 {
69                 private:
70                         std::string inline_base_name;
71
72                 public:
73                         Loader(Step &);
74                         Loader(Step &, Collection &);
75                 private:
76                         void init();
77
78                 public:
79                         void set_inline_base_name(const std::string &);
80
81                 private:
82                         void blend();
83                         void blend_factors(BlendFactor, BlendFactor);
84                         void depth_test();
85                         void depth_compare(Predicate);
86                         void lighting(const std::string &);
87                         void lighting_inline();
88                         void object(const std::string &);
89                         void scene(const std::string &);
90                         void stencil_test();
91                 };
92
93                 std::string tag;
94                 const Lighting *lighting;
95                 DepthTest depth_test;
96                 StencilTest stencil_test;
97                 Blend blend;
98                 std::string slot_name;
99                 Renderable *default_renderable;
100
101                 ~Step();
102         };
103
104         struct PostProcessor
105         {
106                 GL::PostProcessor::Template *postprocessor_template;
107                 std::string slot_name;
108
109                 PostProcessor(GL::PostProcessor::Template * = 0);
110         };
111
112 private:
113         typedef TypeRegistry<PostProcLoader::AddPostProc, PostProcLoader &> PostProcessorRegistry;
114
115         bool hdr;
116         bool alpha;
117         unsigned required_multisample;
118         unsigned max_multisample;
119         std::vector<Step> steps;
120         std::vector<PostProcessor> postprocessors;
121         bool clear_enabled;
122
123 public:
124         SequenceTemplate();
125         ~SequenceTemplate();
126
127         bool get_hdr() const { return hdr; }
128         bool get_alpha() const { return alpha; }
129         unsigned get_required_multisample() const { return required_multisample; }
130         unsigned get_maximum_multisample() const { return max_multisample; }
131         const std::vector<Step> &get_steps() const { return steps; }
132         const std::vector<PostProcessor> &get_postprocessors() const { return postprocessors; }
133         bool is_clear_enabled() const { return clear_enabled; }
134
135         template<typename T>
136         static void register_postprocessor(const std::string &);
137 private:
138         static PostProcessorRegistry &get_postprocessor_registry();
139 };
140
141 template<typename T>
142 void SequenceTemplate::register_postprocessor(const std::string &kw)
143 {
144         get_postprocessor_registry().register_type<T>(kw);
145 }
146
147 template<typename T>
148 void SequenceTemplate::PostProcLoader::postprocessor()
149 {
150         if(postproc)
151                 throw std::logic_error("Only one postprocessor allowed per slot");
152         RefPtr<typename T::Template> pp = new typename T::Template;
153         load_sub(*pp);
154         postproc = pp;
155         pp = 0;
156         postprocessor_loaded();
157 }
158
159 } // namespace GL
160 } // namespace Msp
161
162 #endif