1 #include <msp/core/hash.h>
2 #include <msp/strings/format.h>
3 #include "basicmaterial.h"
5 #include "pbrmaterial.h"
9 #include "unlitmaterial.h"
16 const Program *Material::create_compatible_shader(DataFile::Collection &coll) const
19 map<string, int> spec_values;
20 fill_program_info(module_name, spec_values);
22 string info = module_name;
23 for(map<string, int>::const_iterator i=spec_values.begin(); i!=spec_values.end(); ++i)
24 info += format(",%s:%d", i->first, i->second);
26 string name = format("_material_%016x.shader", hash64(info));
27 Program *shprog = coll.find<Program>(name);
31 const Module &module = coll.get<Module>(module_name);
32 shprog = new Program(module, spec_values);
35 coll.add(name, shprog);
46 #pragma GCC diagnostic push
47 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
48 void Material::attach_texture_to(const Texture *tex, Texturing &texturing, ProgramData &tex_shdata, const string &name) const
55 if(const Uniform *uni = tex_shdata.find_uniform(name))
56 if(const Uniform1i *uni_int = dynamic_cast<const Uniform1i *>(uni))
57 unit = uni_int->get();
60 unit = texturing.find_free_unit(name);
62 throw runtime_error("no free texunit");
64 texturing.attach(unit, *tex, sampler);
65 tex_shdata.uniform(name, unit);
67 #pragma GCC diagnostic pop
69 Material::MaterialRegistry &Material::get_material_registry()
71 static MaterialRegistry registry;
72 static bool initialized = false;
76 registry.register_type<BasicMaterial>("basic");
77 registry.register_type<PbrMaterial>("pbr");
78 registry.register_type<UnlitMaterial>("unlit");
84 Material::Loader::Loader(Material &m):
85 CollectionObjectLoader(m, 0)
88 Material::Loader::Loader(Material &m, Collection &c):
89 CollectionObjectLoader(m, &c)
92 void Material::Loader::init_actions()
94 add("sampler", &Loader::sampler);
97 void Material::Loader::sampler(const string &name)
99 obj.sampler = &get_collection().get<Sampler>(name);
103 DataFile::Loader::ActionMap Material::GenericLoader::shared_actions;
105 Material::GenericLoader::GenericLoader(DataFile::Collection *c):
110 set_actions(shared_actions);
113 Material::GenericLoader::~GenericLoader()
119 void Material::GenericLoader::init_actions()
121 add("type", &GenericLoader::type);
124 void Material::GenericLoader::type(const DataFile::Symbol &sym)
126 get_material_registry().invoke(sym.name, *this);