-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShaderProgram.hpp
353 lines (309 loc) · 11 KB
/
ShaderProgram.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#pragma once
#include <cassert>
#include <string>
#include <array>
#include <vector>
#include <tuple>
#include <algorithm>
#include <incgraphics.h>
#include <Tuple.hpp>
#include <Shader.hpp>
#include <ShaderAttrib.hpp>
#include <VertexArray.hpp>
namespace gl {
template <typename... ShaderTs>
class ShaderProgram {
using self_t = ShaderProgram<ShaderTs...>;
GLuint programId = 0;
std::tuple<ShaderTs...> shaders;
bool shaderOwnership;
enum class ResourceType {
UNIFORM,
UNIFORM_BLOCK,
PROGRAM_INPUT,
PROGRAM_OUTPUT,
SUBROUTINE,
SUBROUTINE_UNIFORM,
TRANSFORM_FEEDBACK_VARYING,
TRANSFORM_FEEDBACK_BUFFER,
BUFFER_VARIABLE,
SHADER_STORAGE_BLOCK,
};
template <ResourceType rT, ShaderType sT = ShaderType::NO_TYPE>
static constexpr GLenum get_gl_resource_type() {
return 0;
/* switch(rT) { */
/* case ResourceType::UNIFORM:return GL_UNIFORM; */
/* case ResourceType::UNIFORM_BLOCK:return GL_UNIFORM_BLOCK; */
/* case ResourceType::PROGRAM_INPUT:return GL_PROGRAM_INPUT; */
/* case ResourceType::PROGRAM_OUTPUT:return GL_PROGRAM_OUTPUT; */
/* case ResourceType::SUBROUTINE: */
/* switch(sT) { */
/* case ShaderType::VERTEX: return GL_VERTEX_SUBROUTINE; */
/* case ShaderType::TESS_CNTRL: return GL_TESS_CONTROL_SUBROUTINE; */
/* case ShaderType::TESS_EVAL: return GL_TESS_EVALUATION_SUBROUTINE; */
/* case ShaderType::GEOMETRY: return GL_GEOMETRY_SUBROUTINE; */
/* case ShaderType::FRAGMENT: return GL_FRAGMENT_SUBROUTINE; */
/* case ShaderType::COMPUTE: return GL_COMPUTE_SUBROUTINE; */
/* } */
/* case ResourceType::SUBROUTINE_UNIFORM: */
/* switch(sT) { */
/* case ShaderType::VERTEX: return GL_VERTEX_SUBROUTINE_UNIFORM; */
/* case ShaderType::TESS_CNTRL: return GL_TESS_CONTROL_SUBROUTINE_UNIFORM; */
/* case ShaderType::TESS_EVAL: return GL_TESS_EVALUATION_SUBROUTINE_UNIFORM; */
/* case ShaderType::GEOMETRY: return GL_GEOMETRY_SUBROUTINE_UNIFORM; */
/* case ShaderType::FRAGMENT: return GL_FRAGMENT_SUBROUTINE_UNIFORM; */
/* case ShaderType::COMPUTE: return GL_COMPUTE_SUBROUTINE_UNIFORM; */
/* } */
/* case ResourceType::TRANSFORM_FEEDBACK_VARYING:return GL_TRANSFORM_FEEDBACK_VARYING; */
/* case ResourceType::TRANSFORM_FEEDBACK_BUFFER:return GL_TRANSFORM_FEEDBACK_BUFFER; */
/* case ResourceType::BUFFER_VARIABLE:return GL_BUFFER_VARIABLE; */
/* case ResourceType::SHADER_STORAGE_BLOCK:return GL_SHADER_STORAGE_BLOCK; */
/* } */
}
public:
struct Binary {
int size;
GLenum format;
void *data = nullptr;
Binary(ShaderProgram<ShaderTs...> &program) {
size = program.get<GL_PROGRAM_BINARY_LENGTH>();
data = malloc(size);
GLint written_bytes;
glGetProgramBinary(program.id(), size, &written_bytes, &format, data); GLERROR
ASSERT(size == written_bytes);
}
~Binary() {
free(data);
}
};
static void compile_program(ShaderProgram<ShaderTs...> &program) {
program.compile_program();
}
void compile_program() {
Tuple::for_each(shaders, [&](auto &s) mutable -> void {
Logger::Debug("init shader '%s'\n", s.file.name().c_str());
if(shaderOwnership) {
s.init();
}
});
programId = glCreateProgram(); GLERROR
ASSERT(this->programId != 0);
Tuple::for_each(shaders, [&](auto &s) mutable -> void {
Logger::Debug("attach shader '%s'\n", s.file.name().c_str());
glAttachShader(programId, s.id()); GLERROR
});
Logger::Debug("link program\n");
glLinkProgram(programId); GLERROR
Tuple::for_each(shaders, [&](auto &s) mutable -> void {
Logger::Debug("clear shader '%s'\n", s.file.name().c_str());
if(shaderOwnership) {
s.clear();
}
});
ASSERT(this->is_valid());
}
void bind_attrib(int index, const std::string &location) {
glBindAttribLocation(programId, index, location.c_str()); GLERROR
}
template <typename... STRINGs>
ShaderProgram(STRINGs&&... shader_filenames):
shaders(std::forward<STRINGs>(shader_filenames)...),
shaderOwnership(true)
{}
GLuint id() const {
return programId;
}
template <GLenum Parameter>
int get() {
GLint res;
glGetProgramiv(id(), Parameter, &res); GLERROR
return res;
}
Binary get_binary() {
return Binary(*this);
}
template <ResourceType ResourceT, ShaderType ShaderT>
int get_resource_index(std::string name) {
constexpr GLenum vartype = get_gl_resource_type<ResourceT, ShaderT>();
auto ind = glGetProgramResourceIndex(id(), vartype, name.c_str()); GLERROR
return ind;
}
template <ResourceType ResourceT, ShaderType ShaderT>
std::string get_resource_name(int index) {
char name[255];
constexpr GLenum vartype = get_gl_resource_type<ResourceT, ShaderT>();
int len;
glGetProgramResourceIndex(id(), vartype, index, 255, &len, name); GLERROR
return name;
}
template <typename... AttribTs>
static void init(self_t &program, gl::VertexArray<AttribTs ...> &vao) {
program.init(vao);
}
template <typename... AttribTs>
void init(gl::VertexArray<AttribTs...> &vao) {
gl::VertexArray<AttribTs...>::bind(vao);
this->compile_program();
int i = 0;
Tuple::for_each(vao.attributes, [&](const auto &attrib) -> void {
this->bind_attrib(i, attrib.location);
++i;
});
gl::VertexArray<AttribTs...>::unbind();
}
static void use(GLuint progId) {
glUseProgram(progId); GLERROR
}
static void use(self_t &program) {
program.use();
}
void use() {
this->use(id());
}
template <typename ShaderUniformT>
int assign_uniform(ShaderUniformT &uniform) const {
uniform.set_id(this->id());
Logger::Debug("%d <- %s\n", int(this->id()), uniform.str().c_str());
return 0;
}
template <typename... ShaderUniformTs>
void assign_uniforms(ShaderUniformTs& ...uniforms) const {
unroll(assign_uniform(uniforms)...);
}
template <typename ShaderUniformT>
static int unassign_uniform(ShaderUniformT &uniform) {
uniform.unset_id();
return 0;
}
template <typename... ShaderUniformTs>
static void unassign_uniforms(ShaderUniformTs& ...uniforms) {
unroll(unassign_uniform(uniforms)...);
}
static void dispatch(size_t x, size_t y, size_t z) {
glDispatchCompute(x, y, z); GLERROR
}
static glm::ivec3 get_max_wgsize(bool xyz=true) {
glm::ivec3 wg_size(0, 0, 0);
glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 0, &wg_size.x); GLERROR
glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 1, &wg_size.y); GLERROR
glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 2, &wg_size.z); GLERROR
return wg_size;
}
static int get_max_wg_invocations() {
int wg_invocations;
glGetIntegerv(GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS, &wg_invocations); GLERROR
return wg_invocations;
}
static void print_compute_capabilities() {
int wg_size[3];
glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 0, &wg_size[0]); GLERROR
glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 1, &wg_size[1]); GLERROR
glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 2, &wg_size[2]); GLERROR
Logger::Info("max work group sizes: [%d %d %d]\n", wg_size[0], wg_size[1], wg_size[2]);
int wg_invocations;
glGetIntegerv(GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS, &wg_invocations); GLERROR
Logger::Info("max work group invocations: %d\n", wg_invocations);
}
static void barrier(GLenum barrier_bits) {
glMemoryBarrier(barrier_bits); GLERROR
}
static void unuse() {
glUseProgram(0); GLERROR
}
static void clear(self_t &program) {
program.clear();
}
void clear() {
Tuple::for_each(shaders, [&](const auto &s) {
glDetachShader(programId, s.id()); GLERROR
});
glDeleteProgram(programId); GLERROR
}
bool is_valid() {
glValidateProgram(programId); GLERROR
int params = get<GL_VALIDATE_STATUS>();
Logger::Info("programId %d GL_VALIDATE_STATUS = %d\n", programId, params);
print_info_log();
print_all();
return params == GL_TRUE;
}
static const char* GL_type_to_string(GLenum type) {
switch(type) {
case GL_BOOL: return "bool";
case GL_INT: return "int";
case GL_FLOAT: return "float";
case GL_FLOAT_VEC2: return "vec2";
case GL_FLOAT_VEC3: return "vec3";
case GL_FLOAT_VEC4: return "vec4";
case GL_FLOAT_MAT2: return "mat2";
case GL_FLOAT_MAT3: return "mat3";
case GL_FLOAT_MAT4: return "mat4";
case GL_SAMPLER_2D: return "sampler2D";
case GL_SAMPLER_3D: return "sampler3D";
case GL_SAMPLER_CUBE: return "samplerCube";
case GL_SAMPLER_2D_SHADOW: return "sampler2DShadow";
default: break;
}
return "other";
}
void print_info_log() {
int max_length = 2048;
int actual_length = 0;
char programId_log[2048];
glGetProgramInfoLog(programId, max_length, &actual_length, programId_log); GLERROR
Logger::Info("programId info log for GL index %u:\n%s\n", programId, programId_log);
}
void print_all() {
Logger::Info("--------------------\n");
Logger::Info("shader programId %d info:\n", programId);
int params = -1;
params = get<GL_LINK_STATUS>();
Logger::Info("GL_LINK_STATUS = %d\n", params);
params = get<GL_ATTACHED_SHADERS>();
Logger::Info("GL_ATTACHED_SHADERS = %d\n", params);
params = get<GL_ACTIVE_ATTRIBUTES>();
Logger::Info("GL_ACTIVE_ATTRIBUTES = %d\n", params);
for (int i = 0; i < params; i++) {
char name[64];
int max_length = 64;
int actual_length = 0;
int size = 0;
GLenum type;
glGetActiveAttrib(programId, i, max_length, &actual_length, &size, &type, name);
if(size > 1) {
for(int j = 0; j < size; j++) {
char long_name[256];
snprintf(long_name, sizeof(long_name), "%s[%d]", name, j);
int location = glGetAttribLocation(programId, long_name); GLERROR
Logger::Info(" %d) type:%s name:%s location:%d\n", i, GL_type_to_string(type), long_name, location);
}
} else {
int location = glGetAttribLocation(programId, name); GLERROR
Logger::Info(" %d) type:%s name:%s location:%d\n", i, GL_type_to_string(type), name, location);
}
}
params = get<GL_ACTIVE_UNIFORMS>();
Logger::Info("GL_ACTIVE_UNIFORMS = %d\n", params);
for(int i = 0; i < params; i++) {
char name[256];
int max_length = 64;
int actual_length = 0;
int size = 0;
GLenum type;
glGetActiveUniform(programId, i, max_length, &actual_length, &size, &type, name); GLERROR
if(size > 1) {
for(int j = 0; j < size; j++) {
char long_name[512];
sprintf(long_name, "%s[%d]", name, j);
int location = glGetUniformLocation(programId, long_name); GLERROR
Logger::Info(" %d) type:%s name:%s location:%d\n", i, GL_type_to_string(type), long_name, location);
}
} else {
int location = glGetUniformLocation(programId, name); GLERROR
Logger::Info(" %d) type:%s name:%s location:%d\n", i, GL_type_to_string(type), name, location);
}
}
}
};
}