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