]> git.tdb.fi Git - libs/gl.git/blobdiff - source/technique.cpp
Use an explicit material slot name in RenderPass
[libs/gl.git] / source / technique.cpp
index c7bd25ed1a70e51f920a72436255f9070e29aa02..928c4ff6ea8cb4f00d5c6d73af7c1c263e339678 100644 (file)
@@ -1,11 +1,6 @@
-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
-#include <msp/strings/formatter.h>
+#include <msp/core/refptr.h>
+#include <msp/datafile/collection.h>
+#include <msp/strings/format.h>
 #include "material.h"
 #include "program.h"
 #include "programdata.h"
@@ -18,146 +13,128 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
-Technique::Technique():
-       main_texture(0),
-       normal_pass(&passes[0]),
-       material(0)
-{ }
-
-Technique::~Technique()
+RenderPass &Technique::add_pass(const Tag &tag)
 {
-       for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
-               delete i->second.shdata;
+       return insert_unique(passes, tag, RenderPass())->second;
 }
 
-bool Technique::has_pass(const GL::Tag &tag) const
+bool Technique::has_pass(const Tag &tag) const
 {
        return passes.count(tag);
 }
 
-const ObjectPass &Technique::get_pass(const GL::Tag &tag) const
+const RenderPass &Technique::get_pass(const Tag &tag) const
 {
-       PassMap::const_iterator i=passes.find(tag);
-       if(i==passes.end())
-               throw KeyError("Unknown pass");
-       return i->second;
+       return get_item(passes, tag);
 }
 
-unsigned Technique::get_texture_index(const std::string &n) const
+bool Technique::replace_texture(const string &slot, const Texture &tex)
 {
-       for(unsigned i=0; i<textures.size(); ++i)
-               if(textures[i].name==n)
-                       return i;
+       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;
+               }
+       }
 
-       throw KeyError("Unknown texture slot", n);
+       return replaced;
 }
 
-const Texture *Technique::get_texture(unsigned i) const
+bool Technique::replace_material(const string &slot, const Material &mat)
 {
-       if(i>=textures.size())
-               throw KeyError("Texture index out of range");
+       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;
+}
 
-       return textures[i].texture;
+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):
-       tech(t),
-       coll(c)
+       DataFile::CollectionObjectLoader<Technique>(t, &c)
 {
-       add("material",        &Technique::material);
-       add("material_inline", &Loader::material_inline);
-       add("pass",            &Loader::pass);
-       add("shader",          &Loader::shader);
-       add("shader_texture",  &Loader::shader_texture);
-       add("texture",         &Loader::texture);
-       add("texture_slot",    &Loader::texture_slot);
+       init();
 }
 
-void Technique::Loader::finish()
+void Technique::Loader::init()
 {
-       for(PassMap::iterator i=tech.passes.begin(); i!=tech.passes.end(); ++i)
-               if(i->second.shdata)
-               {
-                       for(unsigned j=0; j<tech.textures.size(); ++j)
-                       {
-                               unsigned loc=i->second.shprog->get_uniform_location(tech.textures[j].name);
-                               i->second.shdata->uniform(loc, static_cast<int>(j));
-                       }
-               }
+       add("inherit", &Loader::inherit);
+       add("pass", &Loader::pass);
 }
 
-void Technique::Loader::material_inline()
+void Technique::Loader::inherit(const string &n)
 {
-       RefPtr<Material> mat=new Material;
-       load_sub(*mat);
-       coll.add(format("_%p", mat.get()), mat.get());
-       tech.material=mat.release();
+       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)
 {
-       Tag tag(n);
-       if(tech.passes.count(tag))
-               throw KeyError("Duplicate pass name", n);
-       ObjectPass p;
-       load_sub(p, coll);
-       tech.passes[tag]=p;
+       RenderPass p;
+       if(coll)
+               load_sub(p, get_collection());
+       else
+               load_sub(p);
+
+       insert_unique(obj.passes, n, p);
 }
 
-void Technique::Loader::shader(const string &n)
-{
-       Program *shprog=coll.get<Program>(n);
-       if(shprog)  // Allow for unsupported shaders
-       {
-               RefPtr<ProgramData> shdata=new ProgramData;
-               load_sub(*shdata, *shprog);
 
-               tech.normal_pass->shprog=shprog;
-               if(tech.normal_pass->shdata)
-                       delete tech.normal_pass->shdata;
-               tech.normal_pass->shdata=shdata.release();
-       }
+Technique::InheritLoader::InheritLoader(Technique &t, Collection &c):
+       DataFile::CollectionObjectLoader<Technique>(t, &c)
+{
+       add("material", &InheritLoader::material);
+       add("texture", &InheritLoader::texture);
 }
 
-void Technique::Loader::shader_texture(const string &n)
+void Technique::InheritLoader::material(const string &slot, const string &name)
 {
-       string::size_type eqsign=n.find('=');
-       TextureSlot tex;
-       if(eqsign!=string::npos)
+       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())
        {
-               tex.name=n.substr(0, eqsign);
-               tex.texture=coll.get<Texture>(n.substr(eqsign+1));
+               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
-       {
-               string::size_type dot=n.rfind('.');
-               tex.name=n.substr(0, dot);
-               tex.texture = coll.get<Texture>(n);
-       }
-       for(string::iterator i=tex.name.begin(); i!=tex.name.end(); ++i)
-               if(!isalnum(*i))
-                       *i='_';
-       tech.textures.push_back(tex);
-}
-
-void Technique::Loader::texture(const string &n)
-{
-       if(tech.main_texture)
-               throw Exception("Only one main texture may be specified");
-
-       tech.main_texture=coll.get<Texture>(n);
-       TextureSlot tex;
-       tex.name="texture";
-       tex.texture=tech.main_texture;
-       tech.textures.push_back(tex);
+               pass.set_material(&mat);
 }
 
-void Technique::Loader::texture_slot(const string &n)
+void Technique::InheritLoader::texture(const string &slot, const string &name)
 {
-       TextureSlot tex;
-       tex.name=n;
-       tech.textures.push_back(tex);
+       if(!obj.replace_texture(slot, get_collection().get<Texture>(name)))
+               throw key_error(slot);
 }
 
 } // namespace GL