Migrate from LoadableTypeRegistry to TypeRegistry
[libs/gl.git] / source / materials / material.cpp
1 #include <msp/core/hash.h>
2 #include <msp/strings/format.h>
3 #include "basicmaterial.h"
4 #include "gl.h"
5 #include "pbrmaterial.h"
6 #include "resources.h"
7 #include "texturing.h"
8 #include "uniform.h"
9 #include "unlitmaterial.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 const Program *Material::create_compatible_shader(DataFile::Collection &coll) const
17 {
18         string module_name;
19         map<string, int> spec_values;
20         fill_program_info(module_name, spec_values);
21
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);
25
26         string name = format("_material_%016x.shader", hash64(info));
27         Program *shprog = coll.find<Program>(name);
28         if(shprog)
29                 return shprog;
30
31         const Module &module = coll.get<Module>(module_name);
32         shprog = new Program(module, spec_values);
33         try
34         {
35                 coll.add(name, shprog);
36         }
37         catch(...)
38         {
39                 delete shprog;
40                 throw;
41         }
42
43         return shprog;
44 }
45
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
49 {
50         if(!tex)
51                 return;
52
53         int unit = -1;
54
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();
58
59         if(unit<0)
60                 unit = texturing.find_free_unit(name);
61         if(unit<0)
62                 throw runtime_error("no free texunit");
63
64         texturing.attach(unit, *tex, sampler);
65         tex_shdata.uniform(name, unit);
66 }
67 #pragma GCC diagnostic pop
68
69 Material::MaterialRegistry &Material::get_material_registry()
70 {
71         static MaterialRegistry registry;
72         static bool initialized = false;
73         if(!initialized)
74         {
75                 initialized = true;
76                 registry.register_type<BasicMaterial>("basic");
77                 registry.register_type<PbrMaterial>("pbr");
78                 registry.register_type<UnlitMaterial>("unlit");
79         }
80         return registry;
81 }
82
83
84 Material::Loader::Loader(Material &m):
85         CollectionObjectLoader(m, 0)
86 { }
87
88 Material::Loader::Loader(Material &m, Collection &c):
89         CollectionObjectLoader(m, &c)
90 { }
91
92 void Material::Loader::init_actions()
93 {
94         add("sampler", &Loader::sampler);
95 }
96
97 void Material::Loader::sampler(const string &name)
98 {
99         obj.sampler = &get_collection().get<Sampler>(name);
100 }
101
102
103 DataFile::Loader::ActionMap Material::GenericLoader::shared_actions;
104
105 Material::GenericLoader::GenericLoader(DataFile::Collection *c):
106         coll(c)
107 {
108         set_actions(shared_actions);
109 }
110
111 void Material::GenericLoader::init_actions()
112 {
113         get_material_registry().invoke_all(*this);
114 }
115
116 } // namespace GL
117 } // namespace Msp