1 #include <msp/core/refptr.h>
2 #include <msp/datafile/collection.h>
3 #include <msp/strings/format.h>
6 #include "programdata.h"
16 RenderPass &Technique::add_pass(const Tag &tag)
18 return insert_unique(passes, tag, RenderPass())->second;
21 bool Technique::has_pass(const Tag &tag) const
23 return passes.count(tag);
26 const RenderPass &Technique::get_pass(const Tag &tag) const
28 return get_item(passes, tag);
31 bool Technique::replace_texture(const string &slot, const Texture &tex)
33 bool replaced = false;
34 for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
36 int index = i->second.get_texture_index(slot);
39 i->second.set_texture(index, &tex);
47 bool Technique::replace_material(const string &slot, const Material &mat)
49 bool replaced = false;
50 for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
52 const string &pass_slot = i->second.get_material_slot_name();
53 if(!pass_slot.empty() && pass_slot==slot)
55 i->second.set_material(&mat);
63 bool Technique::replace_uniforms(const ProgramData &shdata)
65 bool replaced = false;
66 const vector<string> &uniform_names = shdata.get_uniform_names();
67 for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
69 RefPtr<ProgramData> new_shdata;
70 for(vector<string>::const_iterator j=uniform_names.begin(); j!=uniform_names.end(); ++j)
72 const string &name = i->second.get_slotted_uniform_name(*j);
77 new_shdata = new ProgramData(*i->second.get_shader_data());
79 new_shdata->uniform(name, shdata.get_uniform(*j));
84 i->second.set_shader_program(i->second.get_shader_program(), new_shdata.get());
90 bool Technique::has_shaders() const
92 for(PassMap::const_iterator i=passes.begin(); i!=passes.end(); ++i)
93 if(i->second.get_shader_program())
99 Technique::Loader::Loader(Technique &t):
100 DataFile::CollectionObjectLoader<Technique>(t, 0)
105 Technique::Loader::Loader(Technique &t, Collection &c):
106 DataFile::CollectionObjectLoader<Technique>(t, &c)
111 void Technique::Loader::init()
113 add("inherit", &Loader::inherit);
114 add("pass", &Loader::pass);
117 void Technique::Loader::inherit(const string &n)
119 obj.passes = get_collection().get<Technique>(n).get_passes();
120 InheritLoader ldr(obj, get_collection());
124 void Technique::Loader::pass(const string &n)
128 load_sub(p, get_collection());
132 insert_unique(obj.passes, n, p);
136 Technique::InheritLoader::InheritLoader(Technique &t, Collection &c):
137 DataFile::CollectionObjectLoader<Technique>(t, &c)
139 add("material", &InheritLoader::material);
140 add("texture", &InheritLoader::texture);
141 add("uniforms", &InheritLoader::uniforms);
144 void Technique::InheritLoader::material(const string &slot, const string &name)
146 const Material &mat = get_collection().get<Material>(name);
147 if(obj.replace_material(slot, mat))
150 // For backwards compatibility
151 RenderPass &pass = get_item(obj.passes, slot);
152 if(const Material *base_mat = pass.get_material())
154 for(PassMap::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
155 if(i->second.get_material()==base_mat)
156 i->second.set_material(&mat);
159 pass.set_material(&mat);
162 void Technique::InheritLoader::texture(const string &slot, const string &name)
164 if(!obj.replace_texture(slot, get_collection().get<Texture>(name)))
165 throw key_error(slot);
168 void Technique::InheritLoader::uniforms()
172 obj.replace_uniforms(shdata);