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 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 string name = format("_material_%016x.shader", hash64(info));
30 Program *shprog = coll.find<Program>(name);
34 const Module &module = coll.get<Module>(module_name);
35 shprog = new Program(module, spec_values);
38 coll.add(name, shprog);
49 #pragma GCC diagnostic push
50 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
51 void Material::attach_texture_to(const Texture *tex, Texturing &texturing, ProgramData &tex_shdata, const string &name) const
58 if(const Uniform *uni = tex_shdata.find_uniform(name))
59 if(const Uniform1i *uni_int = dynamic_cast<const Uniform1i *>(uni))
60 unit = uni_int->get();
63 unit = texturing.find_free_unit(name);
65 throw runtime_error("no free texunit");
67 texturing.attach(unit, *tex, sampler);
68 tex_shdata.uniform(name, unit);
70 #pragma GCC diagnostic pop
72 Material::MaterialRegistry &Material::get_material_registry()
74 static MaterialRegistry registry;
75 static bool initialized = false;
79 registry.register_type<BasicMaterial>("basic");
80 registry.register_type<PbrMaterial>("pbr");
81 registry.register_type<UnlitMaterial>("unlit");
87 Material::Loader::Loader(Material &m):
88 CollectionObjectLoader(m, 0)
91 Material::Loader::Loader(Material &m, Collection &c):
92 CollectionObjectLoader(m, &c)
95 void Material::Loader::init_actions()
97 add("sampler", &Loader::sampler);
100 void Material::Loader::sampler(const string &name)
102 obj.sampler = &get_collection().get<Sampler>(name);
106 DataFile::Loader::ActionMap Material::GenericLoader::shared_actions;
108 Material::GenericLoader::GenericLoader(DataFile::Collection *c):
113 set_actions(shared_actions);
116 Material::GenericLoader::~GenericLoader()
122 void Material::GenericLoader::init_actions()
124 add("type", &GenericLoader::type);
127 void Material::GenericLoader::type(const DataFile::Symbol &sym)
129 get_material_registry().invoke(sym.name, *this);