... | ... |
@@ -41,6 +41,25 @@ public: |
41 | 41 |
std::string const & name, |
42 | 42 |
Value const & value, |
43 | 43 |
bool required = true |
44 |
+ ); |
|
45 |
+ template<typename Value> |
|
46 |
+ Shader & uniform( |
|
47 |
+ std::string const & name, |
|
48 |
+ Value * value, |
|
49 |
+ bool required = true |
|
50 |
+ ) = delete; |
|
51 |
+ |
|
52 |
+ template<typename Value> |
|
53 |
+ static void uniform_buffer( |
|
54 |
+ std::string const & name, |
|
55 |
+ Value const & value, |
|
56 |
+ bool required = true |
|
57 |
+ ); |
|
58 |
+ template<typename Value> |
|
59 |
+ static void uniform_buffer( |
|
60 |
+ std::string const & name, |
|
61 |
+ Value * value, |
|
62 |
+ bool required = true |
|
44 | 63 |
) = delete; |
45 | 64 |
|
46 | 65 |
protected: |
... | ... |
@@ -51,6 +70,19 @@ protected: |
51 | 70 |
bool set; |
52 | 71 |
}; |
53 | 72 |
|
73 |
+ struct UniformBuffer |
|
74 |
+ { |
|
75 |
+ GLuint buffer; |
|
76 |
+ GLsizeiptr size; |
|
77 |
+ GLuint binding; |
|
78 |
+ bool set; |
|
79 |
+ }; |
|
80 |
+ |
|
81 |
+ struct UniformBlock |
|
82 |
+ { |
|
83 |
+ UniformBuffer & buffer; |
|
84 |
+ }; |
|
85 |
+ |
|
54 | 86 |
void validate_() const; |
55 | 87 |
void current_( |
56 | 88 |
std::string const & error |
... | ... |
@@ -66,17 +98,31 @@ protected: |
66 | 98 |
std::string const & name, |
67 | 99 |
bool required |
68 | 100 |
); |
101 |
+ UniformBlock * uniform_block_( |
|
102 |
+ std::string const & error, |
|
103 |
+ std::string const & name, |
|
104 |
+ bool required, |
|
105 |
+ GLsizeiptr size |
|
106 |
+ ); |
|
107 |
+ static UniformBuffer * uniform_buffer_( |
|
108 |
+ std::string const & error, |
|
109 |
+ std::string const & name, |
|
110 |
+ bool required, |
|
111 |
+ GLsizeiptr size |
|
112 |
+ ); |
|
69 | 113 |
|
70 | 114 |
template<typename Value> |
71 | 115 |
using ByName = std::unordered_map<std::string, Value>; |
72 | 116 |
|
73 |
- GLuint program_; |
|
74 |
- std::string program_name_; |
|
75 |
- std::string static root_; |
|
76 |
- Defines static defines_; |
|
77 |
- Locations static verts_; |
|
78 |
- Locations static frags_; |
|
79 |
- ByName<Uniform> uniforms_; |
|
117 |
+ GLuint program_; |
|
118 |
+ std::string program_name_; |
|
119 |
+ std::string static root_; |
|
120 |
+ Defines static defines_; |
|
121 |
+ Locations static verts_; |
|
122 |
+ Locations static frags_; |
|
123 |
+ ByName<Uniform> uniforms_; |
|
124 |
+ ByName<UniformBlock> uniform_blocks_; |
|
125 |
+ ByName<UniformBuffer> static uniform_buffers_; |
|
80 | 126 |
}; |
81 | 127 |
|
82 | 128 |
|
... | ... |
@@ -123,6 +169,49 @@ inline Shader & Shader::use() |
123 | 169 |
} |
124 | 170 |
|
125 | 171 |
|
172 |
+// Uniform template definitions. |
|
173 |
+ |
|
174 |
+#define GLSHADER_UNIFORM_BUFFER_(BLOCK_OR_BUFFER, BUFFER, SET) \ |
|
175 |
+ if (auto block_or_buffer = BLOCK_OR_BUFFER( \ |
|
176 |
+ error, name, required, sizeof(value) \ |
|
177 |
+ )) \ |
|
178 |
+ { \ |
|
179 |
+ glBindBuffer(GL_UNIFORM_BUFFER, block_or_buffer->BUFFER); \ |
|
180 |
+ GLSHADER_DEBUG_(error_(error, "unprocessed previous error");) \ |
|
181 |
+ glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(value), &value); \ |
|
182 |
+ GLSHADER_DEBUG_(error_(error);) \ |
|
183 |
+ GLSHADER_DEBUG_(block_or_buffer->SET = true;) \ |
|
184 |
+ } |
|
185 |
+ |
|
186 |
+template<typename Value> |
|
187 |
+inline void Shader::uniform_buffer( |
|
188 |
+ std::string const & name, |
|
189 |
+ Value const & value, |
|
190 |
+ bool required |
|
191 |
+) |
|
192 |
+{ |
|
193 |
+ GLSHADER_DEBUG_ERROR_( |
|
194 |
+ "Failed to set uniform buffer '" + name + "'" |
|
195 |
+ ) |
|
196 |
+ GLSHADER_UNIFORM_BUFFER_(uniform_buffer_, buffer, set) |
|
197 |
+} |
|
198 |
+ |
|
199 |
+template<typename Value> |
|
200 |
+inline Shader & Shader::uniform( |
|
201 |
+ std::string const & name, |
|
202 |
+ Value const & value, |
|
203 |
+ bool required |
|
204 |
+) |
|
205 |
+{ |
|
206 |
+ GLSHADER_DEBUG_ERROR_( |
|
207 |
+ "Failed to set uniform block '" + name + "' of " + program_name_ |
|
208 |
+ ) |
|
209 |
+ GLSHADER_DEBUG_(current_(error);) |
|
210 |
+ GLSHADER_UNIFORM_BUFFER_(uniform_block_, buffer.buffer, buffer.set) |
|
211 |
+ return *this; |
|
212 |
+} |
|
213 |
+ |
|
214 |
+ |
|
126 | 215 |
// Uniform template specializations. |
127 | 216 |
|
128 | 217 |
#define GLSHADER_UNIFORM_SIGNATURE_(TYPE) \ |
... | ... |
@@ -155,4 +155,23 @@ inline Shader & Shader::use() |
155 | 155 |
} |
156 | 156 |
|
157 | 157 |
|
158 |
+// Uniform scalar template specializations. |
|
159 |
+ |
|
160 |
+#define GLSHADER_UNIFORM_SCALAR_(TYPE, SUFFIX) \ |
|
161 |
+ GLSHADER_UNIFORM( \ |
|
162 |
+ TYPE, \ |
|
163 |
+ glUniform1##SUFFIX( \ |
|
164 |
+ location, value \ |
|
165 |
+ ); \ |
|
166 |
+ ) |
|
167 |
+ |
|
168 |
+GLSHADER_UNIFORM_SCALAR_(bool, i) |
|
169 |
+GLSHADER_UNIFORM_SCALAR_(GLboolean, i) |
|
170 |
+GLSHADER_UNIFORM_SCALAR_(GLint, i) |
|
171 |
+GLSHADER_UNIFORM_SCALAR_(GLuint, ui) |
|
172 |
+GLSHADER_UNIFORM_SCALAR_(GLfloat, f) |
|
173 |
+ |
|
174 |
+GLSHADER_UNIFORM_DELETE(GLdouble) |
|
175 |
+ |
|
176 |
+ |
|
158 | 177 |
#endif // GLSHADER_SHADER_HPP_ |
... | ... |
@@ -5,6 +5,7 @@ |
5 | 5 |
#include <map> |
6 | 6 |
#include <set> |
7 | 7 |
#include <string> |
8 |
+#include <unordered_map> |
|
8 | 9 |
|
9 | 10 |
#include <GL/glew.h> |
10 | 11 |
|
... | ... |
@@ -35,19 +36,47 @@ public: |
35 | 36 |
Shader & validate(); |
36 | 37 |
Shader & use(); |
37 | 38 |
|
39 |
+ template<typename Value> |
|
40 |
+ Shader & uniform( |
|
41 |
+ std::string const & name, |
|
42 |
+ Value const & value, |
|
43 |
+ bool required = true |
|
44 |
+ ) = delete; |
|
45 |
+ |
|
38 | 46 |
protected: |
39 | 47 |
|
48 |
+ struct Uniform |
|
49 |
+ { |
|
50 |
+ GLint location; |
|
51 |
+ bool set; |
|
52 |
+ }; |
|
53 |
+ |
|
40 | 54 |
void validate_() const; |
41 | 55 |
void current_( |
42 | 56 |
std::string const & error |
43 | 57 |
) const; |
44 | 58 |
|
45 |
- GLuint program_; |
|
46 |
- std::string program_name_; |
|
47 |
- std::string static root_; |
|
48 |
- Defines static defines_; |
|
49 |
- Locations static verts_; |
|
50 |
- Locations static frags_; |
|
59 |
+ static void error_( |
|
60 |
+ std::string const & error, |
|
61 |
+ std::string const & error_hint = {} |
|
62 |
+ ); |
|
63 |
+ |
|
64 |
+ Uniform * uniform_( |
|
65 |
+ std::string const & error, |
|
66 |
+ std::string const & name, |
|
67 |
+ bool required |
|
68 |
+ ); |
|
69 |
+ |
|
70 |
+ template<typename Value> |
|
71 |
+ using ByName = std::unordered_map<std::string, Value>; |
|
72 |
+ |
|
73 |
+ GLuint program_; |
|
74 |
+ std::string program_name_; |
|
75 |
+ std::string static root_; |
|
76 |
+ Defines static defines_; |
|
77 |
+ Locations static verts_; |
|
78 |
+ Locations static frags_; |
|
79 |
+ ByName<Uniform> uniforms_; |
|
51 | 80 |
}; |
52 | 81 |
|
53 | 82 |
|
... | ... |
@@ -55,8 +84,12 @@ protected: |
55 | 84 |
|
56 | 85 |
#ifndef NDEBUG |
57 | 86 |
#define GLSHADER_DEBUG_(...) __VA_ARGS__ |
87 |
+ #define GLSHADER_DEBUG_ERROR_(...) \ |
|
88 |
+ auto error = std::string{} + __VA_ARGS__; |
|
58 | 89 |
#else |
59 | 90 |
#define GLSHADER_DEBUG_(...) |
91 |
+ #define GLSHADER_DEBUG_ERROR_(...) \ |
|
92 |
+ auto error = std::string{}; |
|
60 | 93 |
#endif // NDEBUG |
61 | 94 |
|
62 | 95 |
|
... | ... |
@@ -90,4 +123,36 @@ inline Shader & Shader::use() |
90 | 123 |
} |
91 | 124 |
|
92 | 125 |
|
126 |
+// Uniform template specializations. |
|
127 |
+ |
|
128 |
+#define GLSHADER_UNIFORM_SIGNATURE_(TYPE) \ |
|
129 |
+ template<> \ |
|
130 |
+ inline Shader & Shader::uniform( \ |
|
131 |
+ std::string const & name, \ |
|
132 |
+ TYPE const & value, \ |
|
133 |
+ bool required \ |
|
134 |
+ ) |
|
135 |
+ |
|
136 |
+#define GLSHADER_UNIFORM_DELETE(TYPE) \ |
|
137 |
+ GLSHADER_UNIFORM_SIGNATURE_(TYPE) = delete; |
|
138 |
+ |
|
139 |
+#define GLSHADER_UNIFORM(TYPE, CODE) \ |
|
140 |
+ GLSHADER_UNIFORM_SIGNATURE_(TYPE) \ |
|
141 |
+ { \ |
|
142 |
+ GLSHADER_DEBUG_ERROR_( \ |
|
143 |
+ "Failed to set uniform '" + name + "' of " + program_name_ \ |
|
144 |
+ ) \ |
|
145 |
+ GLSHADER_DEBUG_(current_(error);) \ |
|
146 |
+ if (auto * uniform = uniform_(error, name, required)) \ |
|
147 |
+ { \ |
|
148 |
+ GLint const & location = uniform->location; \ |
|
149 |
+ GLSHADER_DEBUG_(error_(error, "unprocessed previous error");) \ |
|
150 |
+ CODE \ |
|
151 |
+ GLSHADER_DEBUG_(error_(error, "wrong type?");) \ |
|
152 |
+ GLSHADER_DEBUG_(uniform->set = true;) \ |
|
153 |
+ } \ |
|
154 |
+ return *this; \ |
|
155 |
+ } |
|
156 |
+ |
|
157 |
+ |
|
93 | 158 |
#endif // GLSHADER_SHADER_HPP_ |
... | ... |
@@ -23,9 +23,12 @@ public: |
23 | 23 |
Shader & operator=(Shader const &) = delete; |
24 | 24 |
|
25 | 25 |
using Defines = std::map<std::string, std::string>; |
26 |
+ using Locations = std::map<std::string, GLuint>; |
|
26 | 27 |
|
27 | 28 |
static void root(std::string const & root); |
28 | 29 |
static void defines(Defines const & defines); |
30 |
+ static void verts(Locations const & verts); |
|
31 |
+ static void frags(Locations const & frags); |
|
29 | 32 |
|
30 | 33 |
GLuint program() const; |
31 | 34 |
|
... | ... |
@@ -43,6 +46,8 @@ protected: |
43 | 46 |
std::string program_name_; |
44 | 47 |
std::string static root_; |
45 | 48 |
Defines static defines_; |
49 |
+ Locations static verts_; |
|
50 |
+ Locations static frags_; |
|
46 | 51 |
}; |
47 | 52 |
|
48 | 53 |
|
... | ... |
@@ -64,6 +69,8 @@ protected: |
64 | 69 |
} |
65 | 70 |
GLSHADER_SET_(std::string, root) |
66 | 71 |
GLSHADER_SET_(Defines, defines) |
72 |
+GLSHADER_SET_(Locations, verts) |
|
73 |
+GLSHADER_SET_(Locations, frags) |
|
67 | 74 |
|
68 | 75 |
inline GLuint Shader::program() const |
69 | 76 |
{ |
... | ... |
@@ -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 |
{ |
... | ... |
@@ -26,10 +26,14 @@ public: |
26 | 26 |
GLuint program() const; |
27 | 27 |
|
28 | 28 |
Shader & validate(); |
29 |
+ Shader & use(); |
|
29 | 30 |
|
30 | 31 |
protected: |
31 | 32 |
|
32 | 33 |
void validate_() const; |
34 |
+ void current_( |
|
35 |
+ std::string const & error |
|
36 |
+ ) const; |
|
33 | 37 |
|
34 | 38 |
GLuint program_; |
35 | 39 |
std::string program_name_; |
... | ... |
@@ -66,5 +70,11 @@ inline Shader & Shader::validate() |
66 | 70 |
return *this; |
67 | 71 |
} |
68 | 72 |
|
73 |
+inline Shader & Shader::use() |
|
74 |
+{ |
|
75 |
+ glUseProgram(program_); |
|
76 |
+ return *this; |
|
77 |
+} |
|
78 |
+ |
|
69 | 79 |
|
70 | 80 |
#endif // GLSHADER_SHADER_HPP_ |
... | ... |
@@ -25,14 +25,27 @@ public: |
25 | 25 |
|
26 | 26 |
GLuint program() const; |
27 | 27 |
|
28 |
+ Shader & validate(); |
|
29 |
+ |
|
28 | 30 |
protected: |
29 | 31 |
|
32 |
+ void validate_() const; |
|
33 |
+ |
|
30 | 34 |
GLuint program_; |
31 | 35 |
std::string program_name_; |
32 | 36 |
std::string static root_; |
33 | 37 |
}; |
34 | 38 |
|
35 | 39 |
|
40 |
+// Debug macros. |
|
41 |
+ |
|
42 |
+#ifndef NDEBUG |
|
43 |
+ #define GLSHADER_DEBUG_(...) __VA_ARGS__ |
|
44 |
+#else |
|
45 |
+ #define GLSHADER_DEBUG_(...) |
|
46 |
+#endif // NDEBUG |
|
47 |
+ |
|
48 |
+ |
|
36 | 49 |
// Inline definitions. |
37 | 50 |
|
38 | 51 |
#define GLSHADER_SET_(TYPE, NAME) \ |
... | ... |
@@ -47,5 +60,11 @@ inline GLuint Shader::program() const |
47 | 60 |
return program_; |
48 | 61 |
} |
49 | 62 |
|
63 |
+inline Shader & Shader::validate() |
|
64 |
+{ |
|
65 |
+ GLSHADER_DEBUG_(validate_();) |
|
66 |
+ return *this; |
|
67 |
+} |
|
68 |
+ |
|
50 | 69 |
|
51 | 70 |
#endif // GLSHADER_SHADER_HPP_ |
... | ... |
@@ -0,0 +1,51 @@ |
1 |
+#ifndef GLSHADER_SHADER_HPP_ |
|
2 |
+#define GLSHADER_SHADER_HPP_ |
|
3 |
+ |
|
4 |
+ |
|
5 |
+#include <set> |
|
6 |
+#include <string> |
|
7 |
+ |
|
8 |
+#include <GL/glew.h> |
|
9 |
+ |
|
10 |
+ |
|
11 |
+class Shader |
|
12 |
+{ |
|
13 |
+public: |
|
14 |
+ |
|
15 |
+ using Paths = std::set<std::string>; |
|
16 |
+ |
|
17 |
+ explicit Shader(Paths const & paths); |
|
18 |
+ virtual ~Shader(); |
|
19 |
+ Shader(Shader &&) noexcept; |
|
20 |
+ Shader(Shader const &) = delete; |
|
21 |
+ Shader & operator=(Shader &&) = delete; |
|
22 |
+ Shader & operator=(Shader const &) = delete; |
|
23 |
+ |
|
24 |
+ static void root(std::string const & root); |
|
25 |
+ |
|
26 |
+ GLuint program() const; |
|
27 |
+ |
|
28 |
+protected: |
|
29 |
+ |
|
30 |
+ GLuint program_; |
|
31 |
+ std::string program_name_; |
|
32 |
+ std::string static root_; |
|
33 |
+}; |
|
34 |
+ |
|
35 |
+ |
|
36 |
+// Inline definitions. |
|
37 |
+ |
|
38 |
+#define GLSHADER_SET_(TYPE, NAME) \ |
|
39 |
+ inline void Shader::NAME(TYPE const & NAME) \ |
|
40 |
+ { \ |
|
41 |
+ NAME##_ = NAME; \ |
|
42 |
+ } |
|
43 |
+GLSHADER_SET_(std::string, root) |
|
44 |
+ |
|
45 |
+inline GLuint Shader::program() const |
|
46 |
+{ |
|
47 |
+ return program_; |
|
48 |
+} |
|
49 |
+ |
|
50 |
+ |
|
51 |
+#endif // GLSHADER_SHADER_HPP_ |