]> git.tdb.fi Git - libs/gl.git/blobdiff - source/materials/technique.cpp
Rearrange soucre files into subdirectories
[libs/gl.git] / source / materials / technique.cpp
diff --git a/source/materials/technique.cpp b/source/materials/technique.cpp
new file mode 100644 (file)
index 0000000..09799ac
--- /dev/null
@@ -0,0 +1,185 @@
+#include <msp/core/refptr.h>
+#include <msp/datafile/collection.h>
+#include <msp/strings/format.h>
+#include "material.h"
+#include "program.h"
+#include "programdata.h"
+#include "tag.h"
+#include "technique.h"
+#include "texture.h"
+
+using namespace std;
+
+namespace Msp {
+namespace GL {
+
+RenderPass &Technique::add_pass(const Tag &tag)
+{
+       return insert_unique(passes, tag, RenderPass())->second;
+}
+
+bool Technique::has_pass(const Tag &tag) const
+{
+       return passes.count(tag);
+}
+
+const RenderPass &Technique::get_pass(const Tag &tag) const
+{
+       return get_item(passes, tag);
+}
+
+const RenderPass *Technique::find_pass(const Tag &tag) const
+{
+       PassMap::const_iterator i = passes.find(tag);
+       return (i!=passes.end() ? &i->second : 0);
+}
+
+bool Technique::replace_texture(const string &slot, const Texture &tex)
+{
+       bool replaced = false;
+       for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
+       {
+               int index = i->second.get_texture_index(slot);
+               if(index>=0)
+               {
+                       i->second.set_texture(index, &tex);
+                       replaced = true;
+               }
+       }
+
+       return replaced;
+}
+
+bool Technique::replace_material(const string &slot, const Material &mat)
+{
+       bool replaced = false;
+       for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
+       {
+               const string &pass_slot = i->second.get_material_slot_name();
+               if(!pass_slot.empty() && pass_slot==slot)
+               {
+                       i->second.set_material(&mat);
+                       replaced = true;
+               }
+       }
+
+       return replaced;
+}
+
+bool Technique::replace_uniforms(const ProgramData &shdata)
+{
+       bool replaced = false;
+       const vector<string> &uniform_names = shdata.get_uniform_names();
+       for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
+       {
+               RefPtr<ProgramData> new_shdata;
+               for(vector<string>::const_iterator j=uniform_names.begin(); j!=uniform_names.end(); ++j)
+               {
+                       const string &name = i->second.get_slotted_uniform_name(*j);
+                       if(name.empty())
+                               continue;
+
+                       if(!new_shdata)
+                               new_shdata = new ProgramData(*i->second.get_shader_data());
+
+                       new_shdata->uniform(name, shdata.get_uniform(*j));
+                       replaced = true;
+               }
+
+               if(new_shdata)
+                       i->second.set_shader_program(i->second.get_shader_program(), new_shdata.get());
+       }
+
+       return replaced;
+}
+
+bool Technique::has_shaders() const
+{
+       for(PassMap::const_iterator i=passes.begin(); i!=passes.end(); ++i)
+               if(i->second.get_shader_program())
+                       return true;
+       return false;
+}
+
+
+Technique::Loader::Loader(Technique &t):
+       DataFile::CollectionObjectLoader<Technique>(t, 0)
+{
+       init();
+}
+
+Technique::Loader::Loader(Technique &t, Collection &c):
+       DataFile::CollectionObjectLoader<Technique>(t, &c)
+{
+       init();
+}
+
+void Technique::Loader::init()
+{
+       add("inherit", &Loader::inherit);
+       add("pass", &Loader::pass);
+}
+
+void Technique::Loader::inherit(const string &n)
+{
+       obj.passes = get_collection().get<Technique>(n).get_passes();
+       InheritLoader ldr(obj, get_collection());
+       load_sub_with(ldr);
+}
+
+void Technique::Loader::pass(const string &n)
+{
+       RenderPass p;
+       if(coll)
+               load_sub(p, get_collection());
+       else
+               load_sub(p);
+
+       if(!p.get_shader_program())
+               throw logic_error("no shader program in pass");
+
+       insert_unique(obj.passes, n, p);
+}
+
+
+Technique::InheritLoader::InheritLoader(Technique &t, Collection &c):
+       DataFile::CollectionObjectLoader<Technique>(t, &c)
+{
+       add("material", &InheritLoader::material);
+       add("texture", &InheritLoader::texture);
+       add("uniforms", &InheritLoader::uniforms);
+}
+
+void Technique::InheritLoader::material(const string &slot, const string &name)
+{
+       const Material &mat = get_collection().get<Material>(name);
+       if(obj.replace_material(slot, mat))
+               return;
+
+       // For backwards compatibility
+       RenderPass &pass = get_item(obj.passes, slot);
+       if(const Material *base_mat = pass.get_material())
+       {
+               for(PassMap::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
+                       if(i->second.get_material()==base_mat)
+                               i->second.set_material(&mat);
+       }
+       else
+               pass.set_material(&mat);
+}
+
+void Technique::InheritLoader::texture(const string &slot, const string &name)
+{
+       if(!obj.replace_texture(slot, get_collection().get<Texture>(name)))
+               throw key_error(slot);
+}
+
+void Technique::InheritLoader::uniforms()
+{
+       ProgramData shdata;
+       load_sub(shdata);
+       obj.replace_uniforms(shdata);
+}
+
+} // namespace GL
+} // namespace Msp