]> git.tdb.fi Git - libs/gl.git/blob - source/technique.cpp
Add another constructor to RenderTarget to resolve an ambiguity
[libs/gl.git] / source / technique.cpp
1 #include <msp/core/refptr.h>
2 #include <msp/datafile/collection.h>
3 #include <msp/strings/format.h>
4 #include "material.h"
5 #include "program.h"
6 #include "programdata.h"
7 #include "tag.h"
8 #include "technique.h"
9 #include "texture.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 RenderPass &Technique::add_pass(const Tag &tag)
17 {
18         return insert_unique(passes, tag, RenderPass())->second;
19 }
20
21 bool Technique::has_pass(const Tag &tag) const
22 {
23         return passes.count(tag);
24 }
25
26 const RenderPass &Technique::get_pass(const Tag &tag) const
27 {
28         return get_item(passes, tag);
29 }
30
31 const RenderPass *Technique::find_pass(const Tag &tag) const
32 {
33         PassMap::const_iterator i = passes.find(tag);
34         return (i!=passes.end() ? &i->second : 0);
35 }
36
37 bool Technique::replace_texture(const string &slot, const Texture &tex)
38 {
39         bool replaced = false;
40         for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
41         {
42                 int index = i->second.get_texture_index(slot);
43                 if(index>=0)
44                 {
45                         i->second.set_texture(index, &tex);
46                         replaced = true;
47                 }
48         }
49
50         return replaced;
51 }
52
53 bool Technique::replace_material(const string &slot, const Material &mat)
54 {
55         bool replaced = false;
56         for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
57         {
58                 const string &pass_slot = i->second.get_material_slot_name();
59                 if(!pass_slot.empty() && pass_slot==slot)
60                 {
61                         i->second.set_material(&mat);
62                         replaced = true;
63                 }
64         }
65
66         return replaced;
67 }
68
69 bool Technique::replace_uniforms(const ProgramData &shdata)
70 {
71         bool replaced = false;
72         const vector<string> &uniform_names = shdata.get_uniform_names();
73         for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
74         {
75                 RefPtr<ProgramData> new_shdata;
76                 for(vector<string>::const_iterator j=uniform_names.begin(); j!=uniform_names.end(); ++j)
77                 {
78                         const string &name = i->second.get_slotted_uniform_name(*j);
79                         if(name.empty())
80                                 continue;
81
82                         if(!new_shdata)
83                                 new_shdata = new ProgramData(*i->second.get_shader_data());
84
85                         new_shdata->uniform(name, shdata.get_uniform(*j));
86                         replaced = true;
87                 }
88
89                 if(new_shdata)
90                         i->second.set_shader_program(i->second.get_shader_program(), new_shdata.get());
91         }
92
93         return replaced;
94 }
95
96 bool Technique::has_shaders() const
97 {
98         for(PassMap::const_iterator i=passes.begin(); i!=passes.end(); ++i)
99                 if(i->second.get_shader_program())
100                         return true;
101         return false;
102 }
103
104
105 Technique::Loader::Loader(Technique &t):
106         DataFile::CollectionObjectLoader<Technique>(t, 0)
107 {
108         init();
109 }
110
111 Technique::Loader::Loader(Technique &t, Collection &c):
112         DataFile::CollectionObjectLoader<Technique>(t, &c)
113 {
114         init();
115 }
116
117 void Technique::Loader::init()
118 {
119         add("inherit", &Loader::inherit);
120         add("pass", &Loader::pass);
121 }
122
123 void Technique::Loader::inherit(const string &n)
124 {
125         obj.passes = get_collection().get<Technique>(n).get_passes();
126         InheritLoader ldr(obj, get_collection());
127         load_sub_with(ldr);
128 }
129
130 void Technique::Loader::pass(const string &n)
131 {
132         RenderPass p;
133         if(coll)
134                 load_sub(p, get_collection());
135         else
136                 load_sub(p);
137
138         if(!p.get_shader_program())
139                 throw logic_error("no shader program in pass");
140
141         insert_unique(obj.passes, n, p);
142 }
143
144
145 Technique::InheritLoader::InheritLoader(Technique &t, Collection &c):
146         DataFile::CollectionObjectLoader<Technique>(t, &c)
147 {
148         add("material", &InheritLoader::material);
149         add("texture", &InheritLoader::texture);
150         add("uniforms", &InheritLoader::uniforms);
151 }
152
153 void Technique::InheritLoader::material(const string &slot, const string &name)
154 {
155         const Material &mat = get_collection().get<Material>(name);
156         if(obj.replace_material(slot, mat))
157                 return;
158
159         // For backwards compatibility
160         RenderPass &pass = get_item(obj.passes, slot);
161         if(const Material *base_mat = pass.get_material())
162         {
163                 for(PassMap::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
164                         if(i->second.get_material()==base_mat)
165                                 i->second.set_material(&mat);
166         }
167         else
168                 pass.set_material(&mat);
169 }
170
171 void Technique::InheritLoader::texture(const string &slot, const string &name)
172 {
173         if(!obj.replace_texture(slot, get_collection().get<Texture>(name)))
174                 throw key_error(slot);
175 }
176
177 void Technique::InheritLoader::uniforms()
178 {
179         ProgramData shdata;
180         load_sub(shdata);
181         obj.replace_uniforms(shdata);
182 }
183
184 } // namespace GL
185 } // namespace Msp