1 #include <msp/core/hash.h>
2 #include <msp/strings/format.h>
3 #include "basicmaterial.h"
4 #include "pbrmaterial.h"
7 #include "unlitmaterial.h"
14 const Program *Material::create_compatible_shader(const map<string, int> &extra_spec) const
17 map<string, int> spec_values;
18 fill_program_info(module_name, spec_values);
20 for(const auto &kvp: extra_spec)
21 spec_values[kvp.first] = kvp.second;
23 string info = module_name;
24 for(const auto &kvp: spec_values)
25 info += format(",%s:%d", kvp.first, kvp.second);
27 Resources &res = Resources::get_global();
28 string name = format("_material_%016x.shader", hash64(info));
29 Program *shprog = res.find<Program>(name);
33 const Module &module = res.get<Module>(module_name);
34 shprog = new Program(module, spec_values);
37 res.add(name, shprog);
48 void Material::set_debug_name(const string &name)
51 shdata.set_debug_name(name+" [UBO]");
57 Material::MaterialRegistry &Material::get_material_registry()
59 static MaterialRegistry registry;
60 static bool initialized = false;
64 registry.register_type<BasicMaterial>("basic");
65 registry.register_type<PbrMaterial>("pbr");
66 registry.register_type<UnlitMaterial>("unlit");
72 Material::Loader::Loader(Material &m):
73 CollectionObjectLoader(m, 0)
76 Material::Loader::Loader(Material &m, Collection &c):
77 CollectionObjectLoader(m, &c)
80 void Material::Loader::init_actions()
82 add("sampler", &Loader::sampler);
85 void Material::Loader::sampler(const string &name)
87 obj.sampler = &get_collection().get<Sampler>(name);
91 DataFile::Loader::ActionMap Material::GenericLoader::shared_actions;
93 Material::GenericLoader::GenericLoader(DataFile::Collection *c):
98 set_actions(shared_actions);
101 Material::GenericLoader::~GenericLoader()
107 void Material::GenericLoader::init_actions()
109 add("type", &GenericLoader::type);
112 void Material::GenericLoader::type(const DataFile::Symbol &sym)
114 get_material_registry().invoke(sym.name, *this);