]> git.tdb.fi Git - libs/gl.git/blob - source/materials/material.cpp
b737c03183b504b82592f5959cfd70e80ec10f77
[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 Material::MaterialRegistry &Material::get_material_registry()
73 {
74         static MaterialRegistry registry;
75         static bool initialized = false;
76         if(!initialized)
77         {
78                 initialized = true;
79                 registry.register_type<BasicMaterial>("basic");
80                 registry.register_type<PbrMaterial>("pbr");
81                 registry.register_type<UnlitMaterial>("unlit");
82         }
83         return registry;
84 }
85
86
87 Material::Loader::Loader(Material &m):
88         CollectionObjectLoader(m, 0)
89 { }
90
91 Material::Loader::Loader(Material &m, Collection &c):
92         CollectionObjectLoader(m, &c)
93 { }
94
95 void Material::Loader::init_actions()
96 {
97         add("sampler", &Loader::sampler);
98 }
99
100 void Material::Loader::sampler(const string &name)
101 {
102         obj.sampler = &get_collection().get<Sampler>(name);
103 }
104
105
106 DataFile::Loader::ActionMap Material::GenericLoader::shared_actions;
107
108 Material::GenericLoader::GenericLoader(DataFile::Collection *c):
109         coll(c),
110         material(0),
111         mat_loader(0)
112 {
113         set_actions(shared_actions);
114 }
115
116 Material::GenericLoader::~GenericLoader()
117 {
118         delete material;
119         delete mat_loader;
120 }
121
122 void Material::GenericLoader::init_actions()
123 {
124         add("type", &GenericLoader::type);
125 }
126
127 void Material::GenericLoader::type(const DataFile::Symbol &sym)
128 {
129         get_material_registry().invoke(sym.name, *this);
130 }
131
132 } // namespace GL
133 } // namespace Msp