-
Notifications
You must be signed in to change notification settings - Fork 933
/
shadermanager.hpp
170 lines (138 loc) · 4.51 KB
/
shadermanager.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#ifndef OPENMW_COMPONENTS_SETTINGS_SHADERMANAGER_H
#define OPENMW_COMPONENTS_SETTINGS_SHADERMANAGER_H
#include <filesystem>
#include <fstream>
#include <optional>
#include <unordered_map>
#include <vector>
#include <yaml-cpp/yaml.h>
#include <osg/Vec2f>
#include <osg/Vec3f>
#include <osg/Vec4f>
#include <components/debug/debuglog.hpp>
#include <components/files/conversion.hpp>
#include <components/serialization/osgyaml.hpp>
namespace Settings
{
/*
* Manages the shader.yaml file which is auto-generated and lives next to settings.cfg.
* This YAML file is simply a mapping of technique name to a list of uniforms and their values.
* Currently only vec2f, vec3f, vec4f, int, and float uniforms are supported.
*
* config:
* TECHNIQUE:
* MY_FLOAT: 10.34
* MY_VEC2: [0.23, 0.34]
* TECHNIQUE2:
* MY_VEC3: [0.22, 0.33, 0.20]
*/
class ShaderManager
{
public:
enum class Mode
{
Normal,
Debug
};
ShaderManager() = default;
ShaderManager(ShaderManager const&) = delete;
void operator=(ShaderManager const&) = delete;
static ShaderManager& get()
{
static ShaderManager instance;
return instance;
}
Mode getMode() { return mMode; }
void setMode(Mode mode) { mMode = mode; }
const YAML::Node& getRoot() { return mData; }
template <class T>
bool setValue(const std::string& tname, const std::string& uname, const T& value)
{
if (mData.IsNull())
{
Log(Debug::Warning) << "Failed setting " << tname << ", " << uname
<< " : shader settings failed to load";
return false;
}
mData["config"][tname][uname] = value;
return true;
}
template <class T>
std::optional<T> getValue(const std::string& tname, const std::string& uname)
{
if (mData.IsNull())
{
Log(Debug::Warning) << "Failed getting " << tname << ", " << uname
<< " : shader settings failed to load";
return std::nullopt;
}
try
{
auto value = mData["config"][tname][uname];
if (!value)
return std::nullopt;
return value.as<T>();
}
catch (const YAML::BadConversion&)
{
Log(Debug::Warning) << "Failed retrieving " << tname << ", " << uname
<< " : mismatched types in config file.";
}
return std::nullopt;
}
bool load(const std::filesystem::path& path)
{
mData = YAML::Null;
mPath = path;
Log(Debug::Info) << "Loading shader settings file: " << mPath;
if (!std::filesystem::exists(mPath))
{
std::ofstream fout(mPath);
if (!fout)
{
Log(Debug::Error) << "Failed creating shader settings file: " << mPath;
return false;
}
}
try
{
std::ifstream file{ mPath };
mData = YAML::Load(file);
mData.SetStyle(YAML::EmitterStyle::Block);
if (!mData["config"])
mData["config"] = YAML::Node();
return true;
}
catch (const YAML::Exception& e)
{
Log(Debug::Error) << "Shader settings failed to load, " << e.msg;
}
return false;
}
bool save()
{
if (mData.IsNull())
{
Log(Debug::Error) << "Shader settings failed to load, settings will not be saved: " << mPath;
return false;
}
Log(Debug::Info) << "Saving shader settings file: " << mPath;
YAML::Emitter out;
out.SetMapFormat(YAML::Block);
out << mData;
std::ofstream fout(mPath);
fout << out.c_str();
if (!fout)
{
Log(Debug::Error) << "Failed saving shader settings file: " << mPath;
return false;
}
return true;
}
private:
std::filesystem::path mPath;
YAML::Node mData;
Mode mMode = Mode::Normal;
};
}
#endif