X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fmaterials%2Fmaterial.cpp;h=6d9c0571f429f37e8c2ce265ca8c88e92bfaf70c;hb=9813f8711628a0fbe786406e974dc33546dc9cee;hp=810ecd174d81fb407730b33b0488b456dcee4f86;hpb=7aaec9a70b8d7733429bec043f8e33e02956f266;p=libs%2Fgl.git diff --git a/source/materials/material.cpp b/source/materials/material.cpp index 810ecd17..6d9c0571 100644 --- a/source/materials/material.cpp +++ b/source/materials/material.cpp @@ -1,34 +1,40 @@ #include #include #include "basicmaterial.h" -#include "gl.h" #include "pbrmaterial.h" +#include "program.h" #include "resources.h" -#include "texturing.h" -#include "uniform.h" +#include "unlitmaterial.h" using namespace std; namespace Msp { namespace GL { -Program *Material::create_compatible_shader() const +const Program *Material::create_compatible_shader(const map &extra_spec) const { - return new Program(create_program_source()); -} + string module_name; + map spec_values; + fill_program_info(module_name, spec_values); -const Program *Material::create_compatible_shader(DataFile::Collection &coll) const -{ - string source = create_program_source(); - string name = format("_material_%016x.glsl", hash64(source)); - Program *shprog = coll.find(name); + for(const auto &kvp: extra_spec) + spec_values[kvp.first] = kvp.second; + + string info = module_name; + for(const auto &kvp: spec_values) + info += format(",%s:%d", kvp.first, kvp.second); + + Resources &res = Resources::get_global(); + string name = format("_material_%016x.shader", hash64(info)); + Program *shprog = res.find(name); if(shprog) return shprog; - shprog = new Program(create_program_source()); + const Module &module = res.get(module_name); + shprog = new Program(module, spec_values); try { - coll.add(name, shprog); + res.add(name, shprog); } catch(...) { @@ -39,44 +45,30 @@ const Program *Material::create_compatible_shader(DataFile::Collection &coll) co return shprog; } -void Material::attach_texture_to(const Texture *tex, Texturing &texturing, ProgramData &tex_shdata, const string &name) const +void Material::set_debug_name(const string &name) { - if(!tex) - return; - - int unit = -1; - - if(const Uniform *uni = tex_shdata.find_uniform(name)) - if(const Uniform1i *uni_int = dynamic_cast(uni)) - unit = uni_int->get(); - - if(unit<0) - unit = texturing.find_free_unit(name); - if(unit<0) - throw runtime_error("no free texunit"); - - texturing.attach(unit, *tex, sampler); - tex_shdata.uniform(name, unit); +#ifdef DEBUG + shdata.set_debug_name(name+" [UBO]"); +#else + (void)name; +#endif } -Material::MaterialRegistry &Material::get_material_registry() +Material::GenericLoader::TypeRegistry &Material::get_material_registry() { - static MaterialRegistry registry; + static GenericLoader::TypeRegistry registry; static bool initialized = false; if(!initialized) { initialized = true; registry.register_type("basic"); registry.register_type("pbr"); + registry.register_type("unlit"); } return registry; } -Material::Loader::Loader(Material &m): - CollectionObjectLoader(m, 0) -{ } - Material::Loader::Loader(Material &m, Collection &c): CollectionObjectLoader(m, &c) { } @@ -86,24 +78,10 @@ void Material::Loader::init_actions() add("sampler", &Loader::sampler); } -void Material::Loader::sampler(const std::string &name) +void Material::Loader::sampler(const string &name) { obj.sampler = &get_collection().get(name); } - -DataFile::Loader::ActionMap Material::GenericLoader::shared_actions; - -Material::GenericLoader::GenericLoader(DataFile::Collection *c): - coll(c) -{ - set_actions(shared_actions); -} - -void Material::GenericLoader::init_actions() -{ - get_material_registry().add_all(*this); -} - } // namespace GL } // namespace Msp