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