]> git.tdb.fi Git - libs/gl.git/blob - source/materials/material.cpp
Add support for alpha to coverage
[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 Material::Material()
15 {
16         set_alpha_cutoff(0.0f);
17         set_alpha_feather(1.0f);
18 }
19
20 const Program *Material::create_compatible_shader(const map<string, int> &extra_spec) const
21 {
22         string module_name;
23         map<string, int> spec_values;
24         if(alpha_cutoff>0.0f)
25                 spec_values["use_alpha_cutoff"] = true;
26
27         fill_program_info(module_name, spec_values);
28
29         for(const auto &kvp: extra_spec)
30                 spec_values[kvp.first] = kvp.second;
31
32         uint64_t info_hash = hash<64>(module_name);
33         for(const auto &kvp: spec_values)
34         {
35                 info_hash = hash_update<64>(info_hash, kvp.first);
36                 info_hash = hash_update<64>(info_hash, kvp.second);
37         }
38
39         Resources &res = Resources::get_global();
40         string name = format("_material_%016x.shader", info_hash);
41         Program *shprog = res.find<Program>(name);
42         if(shprog)
43                 return shprog;
44
45         const Module &module = res.get<Module>(module_name);
46         shprog = new Program(module, spec_values);
47         try
48         {
49                 res.add(name, shprog);
50         }
51         catch(...)
52         {
53                 delete shprog;
54                 throw;
55         }
56
57         return shprog;
58 }
59
60 void Material::set_alpha_cutoff(float a)
61 {
62         alpha_cutoff = a;
63         shdata.uniform("alpha_cutoff.cutoff", a);
64 }
65
66 void Material::set_alpha_feather(float f)
67 {
68         alpha_feather = f;
69         shdata.uniform("alpha_cutoff.feather", f);
70 }
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::GenericLoader::TypeRegistry &Material::get_material_registry()
82 {
83         static GenericLoader::TypeRegistry 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, Collection &c):
97         CollectionObjectLoader(m, &c)
98 { }
99
100 void Material::Loader::init_actions()
101 {
102         add("alpha_cutoff", &Loader::alpha_cutoff);
103         add("alpha_cutoff", &Loader::alpha_cutoff_feather);
104         add("sampler", &Loader::sampler);
105 }
106
107 void Material::Loader::alpha_cutoff(float a)
108 {
109         obj.set_alpha_cutoff(a);
110 }
111
112 void Material::Loader::alpha_cutoff_feather(float a, float f)
113 {
114         obj.set_alpha_cutoff(a);
115         obj.set_alpha_feather(f);
116 }
117
118 void Material::Loader::sampler(const string &name)
119 {
120         obj.sampler = &get_collection().get<Sampler>(name);
121 }
122
123 } // namespace GL
124 } // namespace Msp