X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Ftechnique.cpp;h=d6ec7115f6e703a5b215803952a27716f934e468;hp=4d2377a350549e50641c0e045cc6e88f7e8c6374;hb=1efd559eb32adf48200941402ccefc6b5422aa4c;hpb=be74989b0300adecc0062f701ff987cf79d1935a diff --git a/source/technique.cpp b/source/technique.cpp index 4d2377a3..d6ec7115 100644 --- a/source/technique.cpp +++ b/source/technique.cpp @@ -1,11 +1,6 @@ -/* $Id$ - -This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - -#include +#include +#include +#include #include "material.h" #include "program.h" #include "programdata.h" @@ -18,134 +13,163 @@ using namespace std; namespace Msp { namespace GL { -Technique::Technique(): - main_texture(0), - normal_pass(&passes[0]), - material(0) -{ } +RenderPass &Technique::add_pass(const Tag &tag) +{ + return insert_unique(passes, tag, RenderPass())->second; +} -Technique::~Technique() +bool Technique::has_pass(const Tag &tag) const { - for(map::iterator i=passes.begin(); i!=passes.end(); ++i) - delete i->second.shdata; + return passes.count(tag); } -bool Technique::has_pass(const GL::Tag &tag) const +const RenderPass &Technique::get_pass(const Tag &tag) const { - return passes.count(tag.id); + return get_item(passes, tag); } -const ObjectPass &Technique::get_pass(const GL::Tag &tag) const +bool Technique::replace_texture(const string &slot, const Texture &tex) { - map::const_iterator i=passes.find(tag.id); - if(i==passes.end()) - throw KeyError("Unknown pass"); - return i->second; + 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; } -unsigned Technique::get_texture_index(const std::string &n) const +bool Technique::replace_material(const string &slot, const Material &mat) { - for(unsigned i=0; isecond.get_material_slot_name(); + if(!pass_slot.empty() && pass_slot==slot) + { + i->second.set_material(&mat); + replaced = true; + } + } - throw KeyError("Unknown texture slot", n); + return replaced; } -const Texture *Technique::get_texture(unsigned i) const +bool Technique::replace_uniforms(const ProgramData &shdata) { - if(i>=textures.size()) - throw KeyError("Texture index out of range"); + bool replaced = false; + const vector &uniform_names = shdata.get_uniform_names(); + for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i) + { + RefPtr new_shdata; + for(vector::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 textures[i]; + 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(t, 0) +{ + init(); +} Technique::Loader::Loader(Technique &t, Collection &c): - tech(t), - coll(c) + DataFile::CollectionObjectLoader(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(map::iterator i=tech.passes.begin(); i!=tech.passes.end(); ++i) - if(i->second.shdata) - { - for(unsigned j=0; jsecond.shdata->uniform(i->second.shprog->get_uniform_location(tech.tex_names[j]), static_cast(j)); - } + add("inherit", &Loader::inherit); + add("pass", &Loader::pass); } -void Technique::Loader::material_inline() +void Technique::Loader::inherit(const string &n) { - RefPtr mat=new Material; - load_sub(*mat); - coll.add(format("_%p", mat.get()), mat.get()); - tech.material=mat.release(); + obj.passes = get_collection().get(n).get_passes(); + InheritLoader ldr(obj, get_collection()); + load_sub_with(ldr); } void Technique::Loader::pass(const string &n) { - unsigned id=Tag(n).id; - if(tech.passes.count(id)) - throw KeyError("Duplicate pass name", n); - ObjectPass p; - load_sub(p, coll); - tech.passes[id]=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(n); - if(shprog) // Allow for unsupported shaders - { - RefPtr 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(t, &c) +{ + add("material", &InheritLoader::material); + add("texture", &InheritLoader::texture); + add("uniforms", &InheritLoader::uniforms); } -void Technique::Loader::shader_texture(const string &n) +void Technique::InheritLoader::material(const string &slot, const string &name) { - unsigned eqsign=n.find('='); - if(eqsign!=string::npos) + const Material &mat = get_collection().get(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()) { - tech.textures.push_back(coll.get(n.substr(eqsign+1))); - tech.tex_names.push_back(n.substr(0, eqsign)); + 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 - { - tech.textures.push_back(coll.get(n)); - tech.tex_names.push_back(n); - } + pass.set_material(&mat); } -void Technique::Loader::texture(const string &n) +void Technique::InheritLoader::texture(const string &slot, const string &name) { - if(tech.main_texture) - throw Exception("Only one main texture may be specified"); - - tech.main_texture=coll.get(n); - tech.textures.push_back(tech.main_texture); - tech.tex_names.push_back("texture"); + if(!obj.replace_texture(slot, get_collection().get(name))) + throw key_error(slot); } -void Technique::Loader::texture_slot(const string &n) +void Technique::InheritLoader::uniforms() { - tech.tex_names.push_back(n); - tech.textures.push_back(0); + ProgramData shdata; + load_sub(shdata); + obj.replace_uniforms(shdata); } } // namespace GL