]> git.tdb.fi Git - libs/gl.git/blob - source/materials/material.cpp
a34c0457384025f2af87f91ed45fac78dd042835
[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 "pbrmaterial.h"
5 #include "program.h"
6 #include "resources.h"
7 #include "unlitmaterial.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13
14 const Program *Material::create_compatible_shader(const map<string, int> &extra_spec) const
15 {
16         string module_name;
17         map<string, int> spec_values;
18         fill_program_info(module_name, spec_values);
19
20         for(const auto &kvp: extra_spec)
21                 spec_values[kvp.first] = kvp.second;
22
23         uint64_t info_hash = hash<64>(module_name);
24         for(const auto &kvp: spec_values)
25         {
26                 info_hash = hash_update<64>(info_hash, kvp.first);
27                 info_hash = hash_update<64>(info_hash, kvp.second);
28         }
29
30         Resources &res = Resources::get_global();
31         string name = format("_material_%016x.shader", info_hash);
32         Program *shprog = res.find<Program>(name);
33         if(shprog)
34                 return shprog;
35
36         const Module &module = res.get<Module>(module_name);
37         shprog = new Program(module, spec_values);
38         try
39         {
40                 res.add(name, shprog);
41         }
42         catch(...)
43         {
44                 delete shprog;
45                 throw;
46         }
47
48         return shprog;
49 }
50
51 void Material::set_debug_name(const string &name)
52 {
53 #ifdef DEBUG
54         shdata.set_debug_name(name+" [UBO]");
55 #else
56         (void)name;
57 #endif
58 }
59
60 Material::GenericLoader::TypeRegistry &Material::get_material_registry()
61 {
62         static GenericLoader::TypeRegistry registry;
63         static bool initialized = false;
64         if(!initialized)
65         {
66                 initialized = true;
67                 registry.register_type<BasicMaterial>("basic");
68                 registry.register_type<PbrMaterial>("pbr");
69                 registry.register_type<UnlitMaterial>("unlit");
70         }
71         return registry;
72 }
73
74
75 Material::Loader::Loader(Material &m, Collection &c):
76         CollectionObjectLoader(m, &c)
77 { }
78
79 void Material::Loader::init_actions()
80 {
81         add("sampler", &Loader::sampler);
82 }
83
84 void Material::Loader::sampler(const string &name)
85 {
86         obj.sampler = &get_collection().get<Sampler>(name);
87 }
88
89 } // namespace GL
90 } // namespace Msp