]> git.tdb.fi Git - libs/gl.git/blob - source/materials/material.cpp
Unify the loader wrappers for Material and Scene
[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         string info = module_name;
24         for(const auto &kvp: spec_values)
25                 info += format(",%s:%d", kvp.first, kvp.second);
26
27         Resources &res = Resources::get_global();
28         string name = format("_material_%016x.shader", hash64(info));
29         Program *shprog = res.find<Program>(name);
30         if(shprog)
31                 return shprog;
32
33         const Module &module = res.get<Module>(module_name);
34         shprog = new Program(module, spec_values);
35         try
36         {
37                 res.add(name, shprog);
38         }
39         catch(...)
40         {
41                 delete shprog;
42                 throw;
43         }
44
45         return shprog;
46 }
47
48 void Material::set_debug_name(const string &name)
49 {
50 #ifdef DEBUG
51         shdata.set_debug_name(name+" [UBO]");
52 #else
53         (void)name;
54 #endif
55 }
56
57 Material::MaterialRegistry &Material::get_material_registry()
58 {
59         static MaterialRegistry registry;
60         static bool initialized = false;
61         if(!initialized)
62         {
63                 initialized = true;
64                 registry.register_type<BasicMaterial>("basic");
65                 registry.register_type<PbrMaterial>("pbr");
66                 registry.register_type<UnlitMaterial>("unlit");
67         }
68         return registry;
69 }
70
71
72 Material::Loader::Loader(Material &m):
73         CollectionObjectLoader(m, 0)
74 { }
75
76 Material::Loader::Loader(Material &m, Collection &c):
77         CollectionObjectLoader(m, &c)
78 { }
79
80 void Material::Loader::init_actions()
81 {
82         add("sampler", &Loader::sampler);
83 }
84
85 void Material::Loader::sampler(const string &name)
86 {
87         obj.sampler = &get_collection().get<Sampler>(name);
88 }
89
90
91 DataFile::Loader::ActionMap Material::GenericLoader::shared_actions;
92
93 Material::GenericLoader::GenericLoader():
94         coll(0),
95         material(0),
96         mat_loader(0)
97 {
98         set_actions(shared_actions);
99 }
100
101 Material::GenericLoader::GenericLoader(DataFile::Collection &c):
102         GenericLoader()
103 {
104         coll = &c;
105 }
106
107 Material::GenericLoader::~GenericLoader()
108 {
109         delete material;
110         delete mat_loader;
111 }
112
113 void Material::GenericLoader::init_actions()
114 {
115         add("type", &GenericLoader::type);
116 }
117
118 void Material::GenericLoader::type(const DataFile::Symbol &sym)
119 {
120         get_material_registry().invoke(sym.name, *this);
121 }
122
123 } // namespace GL
124 } // namespace Msp