]> git.tdb.fi Git - libs/gl.git/blob - source/materials/material.cpp
Split reflection data from Program to a separate struct
[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 "program.h"
7 #include "resources.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(const auto &kvp: extra_spec)
23                 spec_values[kvp.first] = kvp.second;
24
25         string info = module_name;
26         for(const auto &kvp: spec_values)
27                 info += format(",%s:%d", kvp.first, kvp.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 void Material::set_debug_name(const string &name)
51 {
52 #ifdef DEBUG
53         shdata.set_debug_name(name+" [UBO]");
54 #else
55         (void)name;
56 #endif
57 }
58
59 Material::MaterialRegistry &Material::get_material_registry()
60 {
61         static MaterialRegistry registry;
62         static bool initialized = false;
63         if(!initialized)
64         {
65                 initialized = true;
66                 registry.register_type<BasicMaterial>("basic");
67                 registry.register_type<PbrMaterial>("pbr");
68                 registry.register_type<UnlitMaterial>("unlit");
69         }
70         return registry;
71 }
72
73
74 Material::Loader::Loader(Material &m):
75         CollectionObjectLoader(m, 0)
76 { }
77
78 Material::Loader::Loader(Material &m, Collection &c):
79         CollectionObjectLoader(m, &c)
80 { }
81
82 void Material::Loader::init_actions()
83 {
84         add("sampler", &Loader::sampler);
85 }
86
87 void Material::Loader::sampler(const string &name)
88 {
89         obj.sampler = &get_collection().get<Sampler>(name);
90 }
91
92
93 DataFile::Loader::ActionMap Material::GenericLoader::shared_actions;
94
95 Material::GenericLoader::GenericLoader(DataFile::Collection *c):
96         coll(c),
97         material(0),
98         mat_loader(0)
99 {
100         set_actions(shared_actions);
101 }
102
103 Material::GenericLoader::~GenericLoader()
104 {
105         delete material;
106         delete mat_loader;
107 }
108
109 void Material::GenericLoader::init_actions()
110 {
111         add("type", &GenericLoader::type);
112 }
113
114 void Material::GenericLoader::type(const DataFile::Symbol &sym)
115 {
116         get_material_registry().invoke(sym.name, *this);
117 }
118
119 } // namespace GL
120 } // namespace Msp