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