]> git.tdb.fi Git - libs/gl.git/blob - source/materials/material.cpp
71610c7cd9174957e0bf650f5f50631c2812ddb7
[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, Collection &c):
73         CollectionObjectLoader(m, &c)
74 { }
75
76 void Material::Loader::init_actions()
77 {
78         add("sampler", &Loader::sampler);
79 }
80
81 void Material::Loader::sampler(const string &name)
82 {
83         obj.sampler = &get_collection().get<Sampler>(name);
84 }
85
86
87 DataFile::Loader::ActionMap Material::GenericLoader::shared_actions;
88
89 Material::GenericLoader::GenericLoader(DataFile::Collection &c):
90         coll(c),
91         material(0),
92         mat_loader(0)
93 {
94         set_actions(shared_actions);
95 }
96
97 Material::GenericLoader::~GenericLoader()
98 {
99         delete material;
100         delete mat_loader;
101 }
102
103 void Material::GenericLoader::init_actions()
104 {
105         add("type", &GenericLoader::type);
106 }
107
108 void Material::GenericLoader::type(const DataFile::Symbol &sym)
109 {
110         get_material_registry().invoke(sym.name, *this);
111 }
112
113 } // namespace GL
114 } // namespace Msp