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(const map<string, int> &extra_spec) const
19 map<string, int> spec_values;
20 fill_program_info(module_name, spec_values);
22 for(map<string, int>::const_iterator i=extra_spec.begin(); i!=extra_spec.end(); ++i)
23 spec_values[i->first] = i->second;
25 string info = module_name;
26 for(map<string, int>::const_iterator i=spec_values.begin(); i!=spec_values.end(); ++i)
27 info += format(",%s:%d", i->first, i->second);
29 Resources &res = Resources::get_global();
30 string name = format("_material_%016x.shader", hash64(info));
31 Program *shprog = res.find<Program>(name);
35 const Module &module = res.get<Module>(module_name);
36 shprog = new Program(module, spec_values);
39 res.add(name, shprog);
50 #pragma GCC diagnostic push
51 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
52 void Material::attach_texture_to(const Texture *tex, Texturing &texturing, ProgramData &tex_shdata, const string &name) const
59 if(const Uniform *uni = tex_shdata.find_uniform(name))
60 if(const Uniform1i *uni_int = dynamic_cast<const Uniform1i *>(uni))
61 unit = uni_int->get();
64 unit = texturing.find_free_unit(name);
66 throw runtime_error("no free texunit");
68 texturing.attach(unit, *tex, sampler);
69 tex_shdata.uniform(name, unit);
71 #pragma GCC diagnostic pop
73 void Material::set_debug_name(const string &name)
76 shdata.set_debug_name(name+" [UBO]");
82 Material::MaterialRegistry &Material::get_material_registry()
84 static MaterialRegistry registry;
85 static bool initialized = false;
89 registry.register_type<BasicMaterial>("basic");
90 registry.register_type<PbrMaterial>("pbr");
91 registry.register_type<UnlitMaterial>("unlit");
97 Material::Loader::Loader(Material &m):
98 CollectionObjectLoader(m, 0)
101 Material::Loader::Loader(Material &m, Collection &c):
102 CollectionObjectLoader(m, &c)
105 void Material::Loader::init_actions()
107 add("sampler", &Loader::sampler);
110 void Material::Loader::sampler(const string &name)
112 obj.sampler = &get_collection().get<Sampler>(name);
116 DataFile::Loader::ActionMap Material::GenericLoader::shared_actions;
118 Material::GenericLoader::GenericLoader(DataFile::Collection *c):
123 set_actions(shared_actions);
126 Material::GenericLoader::~GenericLoader()
132 void Material::GenericLoader::init_actions()
134 add("type", &GenericLoader::type);
137 void Material::GenericLoader::type(const DataFile::Symbol &sym)
139 get_material_registry().invoke(sym.name, *this);