]> git.tdb.fi Git - libs/gl.git/blob - source/builders/sequencetemplate.h
Redesign depth and stencil test and blend state management
[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 multisample(unsigned);
58                 void multisample_range(unsigned, unsigned);
59                 void postprocessor(const std::string &);
60                 void step(const std::string &);
61                 void step_with_slot(const std::string &, const std::string &);
62         };
63
64         struct Step
65         {
66                 class Loader: public DataFile::CollectionObjectLoader<Step>
67                 {
68                 private:
69                         std::string inline_base_name;
70
71                 public:
72                         Loader(Step &);
73                         Loader(Step &, Collection &);
74                 private:
75                         void init();
76
77                 public:
78                         void set_inline_base_name(const std::string &);
79
80                 private:
81                         void blend();
82                         void blend_factors(BlendFactor, BlendFactor);
83                         void depth_test();
84                         void depth_compare(Predicate);
85                         void lighting(const std::string &);
86                         void lighting_inline();
87                         void object(const std::string &);
88                         void scene(const std::string &);
89                         void stencil_test();
90                 };
91
92                 std::string tag;
93                 const Lighting *lighting;
94                 DepthTest depth_test;
95                 StencilTest stencil_test;
96                 Blend blend;
97                 std::string slot_name;
98                 Renderable *default_renderable;
99
100                 ~Step();
101         };
102
103         struct PostProcessor
104         {
105                 GL::PostProcessor::Template *postprocessor_template;
106                 std::string slot_name;
107
108                 PostProcessor(GL::PostProcessor::Template * = 0);
109         };
110
111         typedef std::vector<PostProcessor> PostProcessorArray;
112
113 private:
114         typedef TypeRegistry<PostProcLoader::AddPostProc, PostProcLoader &> PostProcessorRegistry;
115
116         bool hdr;
117         bool alpha;
118         unsigned required_multisample;
119         unsigned max_multisample;
120         std::vector<Step> steps;
121         PostProcessorArray postprocessors;
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 PostProcessorArray &get_postprocessors() const { return postprocessors; }
133
134         template<typename T>
135         static void register_postprocessor(const std::string &);
136 private:
137         static PostProcessorRegistry &get_postprocessor_registry();
138 };
139
140 template<typename T>
141 void SequenceTemplate::register_postprocessor(const std::string &kw)
142 {
143         get_postprocessor_registry().register_type<T>(kw);
144 }
145
146 template<typename T>
147 void SequenceTemplate::PostProcLoader::postprocessor()
148 {
149         if(postproc)
150                 throw std::logic_error("Only one postprocessor allowed per slot");
151         RefPtr<typename T::Template> pp = new typename T::Template;
152         load_sub(*pp);
153         postproc = pp;
154         pp = 0;
155         postprocessor_loaded();
156 }
157
158 } // namespace GL
159 } // namespace Msp
160
161 #endif