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(Tag tag)
18 return insert_unique(passes, tag, RenderPass())->second;
21 bool Technique::has_pass(Tag tag) const
23 return passes.count(tag);
26 const RenderPass &Technique::get_pass(Tag tag) const
28 return get_item(passes, tag);
31 const RenderPass *Technique::find_pass(Tag tag) const
33 PassMap::const_iterator i = passes.find(tag);
34 return (i!=passes.end() ? &i->second : 0);
37 bool Technique::replace_texture(const string &slot, const Texture &tex)
39 bool replaced = false;
40 for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
42 int index = i->second.get_texture_index(slot);
45 i->second.set_texture(index, &tex);
53 bool Technique::replace_material(const string &slot, const Material &mat)
55 bool replaced = false;
56 for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
58 const string &pass_slot = i->second.get_material_slot_name();
59 if(!pass_slot.empty() && pass_slot==slot)
61 i->second.set_material(&mat);
69 bool Technique::replace_uniforms(const ProgramData &shdata)
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)
75 RefPtr<ProgramData> new_shdata;
76 for(vector<Tag>::const_iterator j=uniform_tags.begin(); j!=uniform_tags.end(); ++j)
78 Tag tag = i->second.get_slotted_uniform_tag(*j);
83 new_shdata = new ProgramData(*i->second.get_shader_data());
85 new_shdata->uniform(tag, shdata.get_uniform(*j));
90 i->second.set_shader_program(i->second.get_shader_program(), new_shdata.get());
96 bool Technique::has_shaders() const
98 for(PassMap::const_iterator i=passes.begin(); i!=passes.end(); ++i)
99 if(i->second.get_shader_program())
105 Technique::Loader::Loader(Technique &t):
106 DataFile::CollectionObjectLoader<Technique>(t, 0)
111 Technique::Loader::Loader(Technique &t, Collection &c):
112 DataFile::CollectionObjectLoader<Technique>(t, &c)
117 void Technique::Loader::init()
119 add("inherit", &Loader::inherit);
120 add("pass", &Loader::pass);
123 void Technique::Loader::inherit(const string &n)
125 obj.passes = get_collection().get<Technique>(n).get_passes();
126 InheritLoader ldr(obj, get_collection());
130 void Technique::Loader::pass(const string &n)
134 load_sub(p, get_collection());
138 if(!p.get_shader_program())
139 throw logic_error("no shader program in pass");
141 insert_unique(obj.passes, n, p);
145 Technique::InheritLoader::InheritLoader(Technique &t, Collection &c):
146 DataFile::CollectionObjectLoader<Technique>(t, &c)
148 add("material", &InheritLoader::material);
149 add("texture", &InheritLoader::texture);
150 add("uniforms", &InheritLoader::uniforms);
153 void Technique::InheritLoader::material(const string &slot, const string &name)
155 const Material &mat = get_collection().get<Material>(name);
156 if(obj.replace_material(slot, mat))
159 // For backwards compatibility
160 RenderPass &pass = get_item(obj.passes, slot);
161 if(const Material *base_mat = pass.get_material())
163 for(PassMap::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
164 if(i->second.get_material()==base_mat)
165 i->second.set_material(&mat);
168 pass.set_material(&mat);
171 void Technique::InheritLoader::texture(const string &slot, const string &name)
173 if(!obj.replace_texture(slot, get_collection().get<Texture>(name)))
174 throw key_error(slot);
177 void Technique::InheritLoader::uniforms()
181 obj.replace_uniforms(shdata);