]> git.tdb.fi Git - libs/gl.git/blob - source/materials/technique.cpp
b5ed6447c314c3e69be3bfe0fcb0c08be67cfc07
[libs/gl.git] / source / materials / 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(Tag tag)
17 {
18         return insert_unique(passes, tag, RenderPass())->second;
19 }
20
21 bool Technique::has_pass(Tag tag) const
22 {
23         return passes.count(tag);
24 }
25
26 const RenderPass &Technique::get_pass(Tag tag) const
27 {
28         return get_item(passes, tag);
29 }
30
31 const RenderPass *Technique::find_pass(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                 Tag tag = i->second.get_texture_tag(slot);
43                 if(tag.id)
44                 {
45                         i->second.set_texture(tag, &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<Tag> &uniform_tags = shdata.get_uniform_tags();
73         for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
74         {
75                 RefPtr<ProgramData> new_shdata;
76                 for(vector<Tag>::const_iterator j=uniform_tags.begin(); j!=uniform_tags.end(); ++j)
77                 {
78                         Tag tag = i->second.get_slotted_uniform_tag(*j);
79                         if(!tag.id)
80                                 continue;
81
82                         if(!new_shdata)
83                                 new_shdata = new ProgramData(*i->second.get_shader_data());
84
85                         new_shdata->uniform(tag, 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 void Technique::set_debug_name(const std::string &name)
105 {
106 #ifdef DEBUG
107         for(map<Tag, RenderPass>::iterator i=passes.begin(); i!=passes.end(); ++i)
108                 i->second.set_debug_name(format("%s [pass:%s]", name, i->first.str()));
109 #else
110         (void)name;
111 #endif
112 }
113
114
115 DataFile::Loader::ActionMap Technique::Loader::shared_actions;
116
117 Technique::Loader::Loader(Technique &t):
118         DataFile::CollectionObjectLoader<Technique>(t, 0)
119 {
120         set_actions(shared_actions);
121 }
122
123 Technique::Loader::Loader(Technique &t, Collection &c):
124         DataFile::CollectionObjectLoader<Technique>(t, &c)
125 {
126         set_actions(shared_actions);
127 }
128
129 void Technique::Loader::init_actions()
130 {
131         add("inherit", &Loader::inherit);
132         add("pass", &Loader::pass);
133 }
134
135 void Technique::Loader::inherit(const string &n)
136 {
137         obj.passes = get_collection().get<Technique>(n).get_passes();
138         InheritLoader ldr(obj, get_collection());
139         load_sub_with(ldr);
140 }
141
142 void Technique::Loader::pass(const string &n)
143 {
144         RenderPass p;
145         if(coll)
146                 load_sub(p, get_collection());
147         else
148                 load_sub(p);
149
150         if(!p.get_shader_program())
151                 throw logic_error("no shader program in pass");
152
153         insert_unique(obj.passes, n, p);
154 }
155
156
157 Technique::InheritLoader::InheritLoader(Technique &t, Collection &c):
158         DataFile::CollectionObjectLoader<Technique>(t, &c)
159 {
160         add("material", &InheritLoader::material);
161         add("texture", &InheritLoader::texture);
162         add("uniforms", &InheritLoader::uniforms);
163 }
164
165 void Technique::InheritLoader::material(const string &slot, const string &name)
166 {
167         const Material &mat = get_collection().get<Material>(name);
168         if(obj.replace_material(slot, mat))
169                 return;
170
171         // For backwards compatibility
172         RenderPass &pass = get_item(obj.passes, slot);
173         if(const Material *base_mat = pass.get_material())
174         {
175                 for(PassMap::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
176                         if(i->second.get_material()==base_mat)
177                                 i->second.set_material(&mat);
178         }
179         else
180                 pass.set_material(&mat);
181 }
182
183 void Technique::InheritLoader::texture(const string &slot, const string &name)
184 {
185         if(!obj.replace_texture(slot, get_collection().get<Texture>(name)))
186                 throw key_error(slot);
187 }
188
189 void Technique::InheritLoader::uniforms()
190 {
191         ProgramData shdata;
192         load_sub(shdata);
193         obj.replace_uniforms(shdata);
194 }
195
196 } // namespace GL
197 } // namespace Msp