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 Tag tag = i->second.get_texture_tag(slot);
45 i->second.set_texture(tag, &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 DataFile::Loader::ActionMap Technique::Loader::shared_actions;
107 Technique::Loader::Loader(Technique &t):
108 DataFile::CollectionObjectLoader<Technique>(t, 0)
110 set_actions(shared_actions);
113 Technique::Loader::Loader(Technique &t, Collection &c):
114 DataFile::CollectionObjectLoader<Technique>(t, &c)
116 set_actions(shared_actions);
119 void Technique::Loader::init_actions()
121 add("inherit", &Loader::inherit);
122 add("pass", &Loader::pass);
125 void Technique::Loader::inherit(const string &n)
127 obj.passes = get_collection().get<Technique>(n).get_passes();
128 InheritLoader ldr(obj, get_collection());
132 void Technique::Loader::pass(const string &n)
136 load_sub(p, get_collection());
140 if(!p.get_shader_program())
141 throw logic_error("no shader program in pass");
143 insert_unique(obj.passes, n, p);
147 Technique::InheritLoader::InheritLoader(Technique &t, Collection &c):
148 DataFile::CollectionObjectLoader<Technique>(t, &c)
150 add("material", &InheritLoader::material);
151 add("texture", &InheritLoader::texture);
152 add("uniforms", &InheritLoader::uniforms);
155 void Technique::InheritLoader::material(const string &slot, const string &name)
157 const Material &mat = get_collection().get<Material>(name);
158 if(obj.replace_material(slot, mat))
161 // For backwards compatibility
162 RenderPass &pass = get_item(obj.passes, slot);
163 if(const Material *base_mat = pass.get_material())
165 for(PassMap::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
166 if(i->second.get_material()==base_mat)
167 i->second.set_material(&mat);
170 pass.set_material(&mat);
173 void Technique::InheritLoader::texture(const string &slot, const string &name)
175 if(!obj.replace_texture(slot, get_collection().get<Texture>(name)))
176 throw key_error(slot);
179 void Technique::InheritLoader::uniforms()
183 obj.replace_uniforms(shdata);