... | ... |
@@ -18,10 +18,17 @@ Overview of usage: |
18 | 18 |
#include <glshader.hpp> |
19 | 19 |
|
20 | 20 |
|
21 |
+// Global data. |
|
22 |
+constexpr auto light_count_max = 32; |
|
23 |
+ |
|
24 |
+ |
|
21 | 25 |
int main() |
22 | 26 |
{ |
23 | 27 |
// Global settings. |
24 | 28 |
Shader::root("assets/shaders"); |
29 |
+ Shader::defines({ |
|
30 |
+ {"light_count_max", std::to_string(light_count_max)}, |
|
31 |
+ }); |
|
25 | 32 |
|
26 | 33 |
// Create. |
27 | 34 |
auto player = Shader({ |
... | ... |
@@ -154,6 +161,14 @@ is to use `#version 110` if none is provided.) |
154 | 161 |
|
155 | 162 |
[`#version`]: https://www.khronos.org/opengl/wiki/Core_Language_(GLSL)#Version |
156 | 163 |
|
164 |
+#### `#define`s |
|
165 |
+ |
|
166 |
+[`#define`][]s specified with `Shader::defines(Shader::Defines const & |
|
167 |
+defines)` are injected after the `#version`. `Shader::Defines` is an alias for |
|
168 |
+`std::map<std::string, std::string>`. |
|
169 |
+ |
|
170 |
+[`#define`]: https://www.khronos.org/opengl/wiki/Core_Language_(GLSL)#Preprocessor_directives |
|
171 |
+ |
|
157 | 172 |
## Dependencies |
158 | 173 |
|
159 | 174 |
Public (interface): |
... | ... |
@@ -2,6 +2,7 @@ |
2 | 2 |
#define GLSHADER_SHADER_HPP_ |
3 | 3 |
|
4 | 4 |
|
5 |
+#include <map> |
|
5 | 6 |
#include <set> |
6 | 7 |
#include <string> |
7 | 8 |
|
... | ... |
@@ -21,7 +22,10 @@ public: |
21 | 22 |
Shader & operator=(Shader &&) = delete; |
22 | 23 |
Shader & operator=(Shader const &) = delete; |
23 | 24 |
|
25 |
+ using Defines = std::map<std::string, std::string>; |
|
26 |
+ |
|
24 | 27 |
static void root(std::string const & root); |
28 |
+ static void defines(Defines const & defines); |
|
25 | 29 |
|
26 | 30 |
GLuint program() const; |
27 | 31 |
|
... | ... |
@@ -38,6 +42,7 @@ protected: |
38 | 42 |
GLuint program_; |
39 | 43 |
std::string program_name_; |
40 | 44 |
std::string static root_; |
45 |
+ Defines static defines_; |
|
41 | 46 |
}; |
42 | 47 |
|
43 | 48 |
|
... | ... |
@@ -58,6 +63,7 @@ protected: |
58 | 63 |
NAME##_ = NAME; \ |
59 | 64 |
} |
60 | 65 |
GLSHADER_SET_(std::string, root) |
66 |
+GLSHADER_SET_(Defines, defines) |
|
61 | 67 |
|
62 | 68 |
inline GLuint Shader::program() const |
63 | 69 |
{ |
... | ... |
@@ -25,6 +25,7 @@ using Here = std::tuple<std::string, int, std::string>; |
25 | 25 |
// NOLINTNEXTLINE |
26 | 26 |
#define GLSHADER_INIT_(NAME, INIT) decltype(NAME) NAME INIT; |
27 | 27 |
GLSHADER_INIT_(Shader::root_, {}) |
28 |
+GLSHADER_INIT_(Shader::defines_, {}) |
|
28 | 29 |
|
29 | 30 |
|
30 | 31 |
template<typename Type> |
... | ... |
@@ -79,7 +80,8 @@ static void info_log_action_( |
79 | 80 |
static std::string source_( |
80 | 81 |
std::string const & error, |
81 | 82 |
std::string const & path, |
82 |
- std::string const & root |
|
83 |
+ std::string const & root, |
|
84 |
+ Shader::Defines const & defines |
|
83 | 85 |
) |
84 | 86 |
{ |
85 | 87 |
// Set here error. |
... | ... |
@@ -154,6 +156,11 @@ static std::string source_( |
154 | 156 |
|
155 | 157 |
// Output. |
156 | 158 |
ostream << line << "\n"; |
159 |
+ for (auto const & define : defines) |
|
160 |
+ ostream |
|
161 |
+ << "#define " |
|
162 |
+ << define.first << " " |
|
163 |
+ << define.second << "\n"; |
|
157 | 164 |
} |
158 | 165 |
|
159 | 166 |
// Non-processed line. |
... | ... |
@@ -263,7 +270,7 @@ Shader::Shader(Paths const & paths) |
263 | 270 |
|
264 | 271 |
// Set shader source. |
265 | 272 |
auto const source_error = STR("Failed to source " << shader_name); |
266 |
- auto const source = source_(source_error, path, root_); |
|
273 |
+ auto const source = source_(source_error, path, root_, defines_); |
|
267 | 274 |
auto const sources = std::array<char const *, 1>{{ |
268 | 275 |
source.c_str() |
269 | 276 |
}}; |
... | ... |
@@ -72,6 +72,12 @@ GLTEST(2, 0, 640, 480, glshader) |
72 | 72 |
"assets/shaders/tests/version_repeated.vert:2: #version 110" |
73 | 73 |
) |
74 | 74 |
|
75 |
+ // Defines. |
|
76 |
+ constexpr auto red = 1.0F; |
|
77 |
+ Shader::defines({ |
|
78 |
+ {"red", std::to_string(red)}, |
|
79 |
+ }); |
|
80 |
+ |
|
75 | 81 |
auto all = Shader({ |
76 | 82 |
"tests/all.vert", |
77 | 83 |
"tests/all.frag", |