]> git.tdb.fi Git - libs/gl.git/blob - source/materials/material.cpp
Set OpenGL debug labels on various objects loaded from Resources
[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 map<string, int> &extra_spec) const
17 {
18         string module_name;
19         map<string, int> spec_values;
20         fill_program_info(module_name, spec_values);
21
22         for(map<string, int>::const_iterator i=extra_spec.begin(); i!=extra_spec.end(); ++i)
23                 spec_values[i->first] = i->second;
24
25         string info = module_name;
26         for(map<string, int>::const_iterator i=spec_values.begin(); i!=spec_values.end(); ++i)
27                 info += format(",%s:%d", i->first, i->second);
28
29         string name = format("_material_%016x.shader", hash64(info));
30         Program *shprog = coll.find<Program>(name);
31         if(shprog)
32                 return shprog;
33
34         const Module &module = coll.get<Module>(module_name);
35         shprog = new Program(module, spec_values);
36         try
37         {
38                 coll.add(name, shprog);
39         }
40         catch(...)
41         {
42                 delete shprog;
43                 throw;
44         }
45
46         return shprog;
47 }
48
49 #pragma GCC diagnostic push
50 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
51 void Material::attach_texture_to(const Texture *tex, Texturing &texturing, ProgramData &tex_shdata, const string &name) const
52 {
53         if(!tex)
54                 return;
55
56         int unit = -1;
57
58         if(const Uniform *uni = tex_shdata.find_uniform(name))
59                 if(const Uniform1i *uni_int = dynamic_cast<const Uniform1i *>(uni))
60                         unit = uni_int->get();
61
62         if(unit<0)
63                 unit = texturing.find_free_unit(name);
64         if(unit<0)
65                 throw runtime_error("no free texunit");
66
67         texturing.attach(unit, *tex, sampler);
68         tex_shdata.uniform(name, unit);
69 }
70 #pragma GCC diagnostic pop
71
72 void Material::set_debug_name(const string &name)
73 {
74 #ifdef DEBUG
75         shdata.set_debug_name(name+" [UBO]");
76 #else
77         (void)name;
78 #endif
79 }
80
81 Material::MaterialRegistry &Material::get_material_registry()
82 {
83         static MaterialRegistry registry;
84         static bool initialized = false;
85         if(!initialized)
86         {
87                 initialized = true;
88                 registry.register_type<BasicMaterial>("basic");
89                 registry.register_type<PbrMaterial>("pbr");
90                 registry.register_type<UnlitMaterial>("unlit");
91         }
92         return registry;
93 }
94
95
96 Material::Loader::Loader(Material &m):
97         CollectionObjectLoader(m, 0)
98 { }
99
100 Material::Loader::Loader(Material &m, Collection &c):
101         CollectionObjectLoader(m, &c)
102 { }
103
104 void Material::Loader::init_actions()
105 {
106         add("sampler", &Loader::sampler);
107 }
108
109 void Material::Loader::sampler(const string &name)
110 {
111         obj.sampler = &get_collection().get<Sampler>(name);
112 }
113
114
115 DataFile::Loader::ActionMap Material::GenericLoader::shared_actions;
116
117 Material::GenericLoader::GenericLoader(DataFile::Collection *c):
118         coll(c),
119         material(0),
120         mat_loader(0)
121 {
122         set_actions(shared_actions);
123 }
124
125 Material::GenericLoader::~GenericLoader()
126 {
127         delete material;
128         delete mat_loader;
129 }
130
131 void Material::GenericLoader::init_actions()
132 {
133         add("type", &GenericLoader::type);
134 }
135
136 void Material::GenericLoader::type(const DataFile::Symbol &sym)
137 {
138         get_material_registry().invoke(sym.name, *this);
139 }
140
141 } // namespace GL
142 } // namespace Msp