1 #include <msp/core/hash.h>
2 #include <msp/strings/format.h>
3 #include "basicmaterial.h"
5 #include "pbrmaterial.h"
8 #include "unlitmaterial.h"
15 const Program *Material::create_compatible_shader(const map<string, int> &extra_spec) const
18 map<string, int> spec_values;
19 fill_program_info(module_name, spec_values);
21 for(const auto &kvp: extra_spec)
22 spec_values[kvp.first] = kvp.second;
24 string info = module_name;
25 for(const auto &kvp: spec_values)
26 info += format(",%s:%d", kvp.first, kvp.second);
28 Resources &res = Resources::get_global();
29 string name = format("_material_%016x.shader", hash64(info));
30 Program *shprog = res.find<Program>(name);
34 const Module &module = res.get<Module>(module_name);
35 shprog = new Program(module, spec_values);
38 res.add(name, shprog);
49 void Material::set_debug_name(const string &name)
52 shdata.set_debug_name(name+" [UBO]");
58 Material::MaterialRegistry &Material::get_material_registry()
60 static MaterialRegistry registry;
61 static bool initialized = false;
65 registry.register_type<BasicMaterial>("basic");
66 registry.register_type<PbrMaterial>("pbr");
67 registry.register_type<UnlitMaterial>("unlit");
73 Material::Loader::Loader(Material &m):
74 CollectionObjectLoader(m, 0)
77 Material::Loader::Loader(Material &m, Collection &c):
78 CollectionObjectLoader(m, &c)
81 void Material::Loader::init_actions()
83 add("sampler", &Loader::sampler);
86 void Material::Loader::sampler(const string &name)
88 obj.sampler = &get_collection().get<Sampler>(name);
92 DataFile::Loader::ActionMap Material::GenericLoader::shared_actions;
94 Material::GenericLoader::GenericLoader(DataFile::Collection *c):
99 set_actions(shared_actions);
102 Material::GenericLoader::~GenericLoader()
108 void Material::GenericLoader::init_actions()
110 add("type", &GenericLoader::type);
113 void Material::GenericLoader::type(const DataFile::Symbol &sym)
115 get_material_registry().invoke(sym.name, *this);