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