1 #include <msp/core/refptr.h>
2 #include <msp/datafile/collection.h>
3 #include <msp/fs/utils.h>
4 #include <msp/strings/format.h>
6 #include "programdata.h"
15 RenderMethod &Technique::add_method(Tag tag)
17 return insert_unique(methods, tag, RenderMethod())->second;
20 bool Technique::has_method(Tag tag) const
22 return methods.count(tag);
25 const RenderMethod &Technique::get_method(Tag tag) const
27 return get_item(methods, tag);
30 const RenderMethod *Technique::find_method(Tag tag) const
32 auto i = methods.find(tag);
33 return (i!=methods.end() ? &i->second : 0);
36 bool Technique::replace_texture(const string &slot, const Texture &tex)
38 bool replaced = false;
39 for(auto &kvp: methods)
41 Tag tag = kvp.second.get_texture_tag(slot);
44 kvp.second.set_texture(tag, &tex);
52 bool Technique::replace_material(const string &slot, const Material &mat)
54 bool replaced = false;
55 for(auto &kvp: methods)
57 const string &pass_slot = kvp.second.get_material_slot_name();
58 if(!pass_slot.empty() && pass_slot==slot)
60 kvp.second.set_material(&mat);
68 bool Technique::replace_uniforms(const ProgramData &shdata)
70 bool replaced = false;
71 const vector<Tag> &uniform_tags = shdata.get_uniform_tags();
72 for(auto &kvp: methods)
74 RefPtr<ProgramData> new_shdata;
75 for(Tag t: uniform_tags)
77 Tag tag = kvp.second.get_slotted_uniform_tag(t);
83 new_shdata = new ProgramData;
84 new_shdata->copy_uniforms(*kvp.second.get_shader_data());
87 new_shdata->copy_uniform(shdata, tag);
92 kvp.second.set_shader_program(kvp.second.get_shader_program(), new_shdata.get());
98 void Technique::set_debug_name(const string &name)
101 for(auto &kvp: methods)
102 kvp.second.set_debug_name(format("%s [method:%s]", name, kvp.first.str()));
109 DataFile::Loader::ActionMap Technique::Loader::shared_actions;
111 Technique::Loader::Loader(Technique &t, Collection &c):
112 DataFile::CollectionObjectLoader<Technique>(t, &c)
114 set_actions(shared_actions);
117 void Technique::Loader::init_actions()
119 add("inherit", &Loader::inherit);
120 add("method", &Loader::method);
123 void Technique::Loader::set_inline_base_name(const string &n)
125 inline_base_name = n;
128 void Technique::Loader::inherit(const string &n)
130 obj.methods = get_collection().get<Technique>(n).get_methods();
131 InheritLoader ldr(obj, get_collection());
135 void Technique::Loader::method(const string &n)
138 RenderMethod::Loader ldr(p, get_collection());
139 ldr.set_inline_base_name(format("%s/%s.method", (inline_base_name.empty() ? FS::basename(get_source()) : inline_base_name), n));
142 if(!p.get_shader_program())
143 throw logic_error("no shader program in method");
145 insert_unique(obj.methods, n, p);
149 Technique::InheritLoader::InheritLoader(Technique &t, Collection &c):
150 DataFile::CollectionObjectLoader<Technique>(t, &c)
152 add("material", &InheritLoader::material);
153 add("texture", &InheritLoader::texture);
154 add("uniforms", &InheritLoader::uniforms);
157 void Technique::InheritLoader::material(const string &slot, const string &name)
159 const Material &mat = get_collection().get<Material>(name);
160 if(obj.replace_material(slot, mat))
163 // For backwards compatibility
164 RenderMethod &method = get_item(obj.methods, slot);
165 if(const Material *base_mat = method.get_material())
167 for(auto &kvp: obj.methods)
168 if(kvp.second.get_material()==base_mat)
169 kvp.second.set_material(&mat);
172 method.set_material(&mat);
175 void Technique::InheritLoader::texture(const string &slot, const string &name)
177 if(!obj.replace_texture(slot, get_collection().get<Texture>(name)))
178 throw key_error(slot);
181 void Technique::InheritLoader::uniforms()
185 obj.replace_uniforms(shdata);