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