]> git.tdb.fi Git - libs/gl.git/blob - source/materials/technique.cpp
268149f8cb4bff921d9459d2e721e99235fd6257
[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 "programdata.h"
7 #include "technique.h"
8 #include "texture.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GL {
14
15 RenderMethod &Technique::add_method(Tag tag)
16 {
17         return insert_unique(methods, tag, RenderMethod())->second;
18 }
19
20 bool Technique::has_method(Tag tag) const
21 {
22         return methods.count(tag);
23 }
24
25 const RenderMethod &Technique::get_method(Tag tag) const
26 {
27         return get_item(methods, tag);
28 }
29
30 const RenderMethod *Technique::find_method(Tag tag) const
31 {
32         auto i = methods.find(tag);
33         return (i!=methods.end() ? &i->second : 0);
34 }
35
36 bool Technique::replace_texture(const string &slot, const Texture &tex)
37 {
38         bool replaced = false;
39         for(auto &kvp: methods)
40         {
41                 Tag tag = kvp.second.get_texture_tag(slot);
42                 if(tag.id)
43                 {
44                         kvp.second.set_texture(tag, &tex);
45                         replaced = true;
46                 }
47         }
48
49         return replaced;
50 }
51
52 bool Technique::replace_material(const string &slot, const Material &mat)
53 {
54         bool replaced = false;
55         for(auto &kvp: methods)
56         {
57                 const string &pass_slot = kvp.second.get_material_slot_name();
58                 if(!pass_slot.empty() && pass_slot==slot)
59                 {
60                         kvp.second.set_material(&mat);
61                         replaced = true;
62                 }
63         }
64
65         return replaced;
66 }
67
68 bool Technique::replace_uniforms(const ProgramData &shdata)
69 {
70         bool replaced = false;
71         const vector<Tag> &uniform_tags = shdata.get_uniform_tags();
72         for(auto &kvp: methods)
73         {
74                 RefPtr<ProgramData> new_shdata;
75                 for(Tag t: uniform_tags)
76                 {
77                         Tag tag = kvp.second.get_slotted_uniform_tag(t);
78                         if(!tag.id)
79                                 continue;
80
81                         if(!new_shdata)
82                                 new_shdata = new ProgramData(*kvp.second.get_shader_data());
83
84                         new_shdata->copy_uniform(shdata, tag);
85                         replaced = true;
86                 }
87
88                 if(new_shdata)
89                         kvp.second.set_shader_program(kvp.second.get_shader_program(), new_shdata.get());
90         }
91
92         return replaced;
93 }
94
95 void Technique::set_debug_name(const string &name)
96 {
97 #ifdef DEBUG
98         for(auto &kvp: methods)
99                 kvp.second.set_debug_name(format("%s [method:%s]", name, kvp.first.str()));
100 #else
101         (void)name;
102 #endif
103 }
104
105
106 DataFile::Loader::ActionMap Technique::Loader::shared_actions;
107
108 Technique::Loader::Loader(Technique &t):
109         DataFile::CollectionObjectLoader<Technique>(t, 0)
110 {
111         set_actions(shared_actions);
112 }
113
114 Technique::Loader::Loader(Technique &t, Collection &c):
115         DataFile::CollectionObjectLoader<Technique>(t, &c)
116 {
117         set_actions(shared_actions);
118 }
119
120 void Technique::Loader::init_actions()
121 {
122         add("inherit", &Loader::inherit);
123         add("method", &Loader::method);
124 }
125
126 void Technique::Loader::set_inline_base_name(const string &n)
127 {
128         inline_base_name = n;
129 }
130
131 void Technique::Loader::inherit(const string &n)
132 {
133         obj.methods = get_collection().get<Technique>(n).get_methods();
134         InheritLoader ldr(obj, get_collection());
135         load_sub_with(ldr);
136 }
137
138 void Technique::Loader::method(const string &n)
139 {
140         RenderMethod p;
141         if(coll)
142         {
143                 RenderMethod::Loader ldr(p, get_collection());
144                 ldr.set_inline_base_name(format("%s/%s.method", (inline_base_name.empty() ? FS::basename(get_source()) : inline_base_name), n));
145                 load_sub_with(ldr);
146         }
147         else
148                 load_sub(p);
149
150         if(!p.get_shader_program())
151                 throw logic_error("no shader program in method");
152
153         insert_unique(obj.methods, 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         RenderMethod &method = get_item(obj.methods, slot);
173         if(const Material *base_mat = method.get_material())
174         {
175                 for(auto &kvp: obj.methods)
176                         if(kvp.second.get_material()==base_mat)
177                                 kvp.second.set_material(&mat);
178         }
179         else
180                 method.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