forked from ixchow/kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGLProgram.cpp
151 lines (135 loc) · 6.34 KB
/
GLProgram.cpp
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
#include "GLProgram.hpp"
#include <iostream>
#include <vector>
GLShader::GLShader( GLenum type, std::string const &source ) {
shader = glCreateShader(type);
GLchar const *str = source.c_str();
GLint length = source.size();
glShaderSource(shader, 1, &str, &length);
glCompileShader(shader);
GLint compile_status = GL_FALSE;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);
if (compile_status != GL_TRUE) {
std::cerr << "Failed to compile shader." << std::endl;
GLint info_log_length = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_log_length);
std::vector< GLchar > info_log(info_log_length, 0);
GLsizei length = 0;
glGetShaderInfoLog(shader, info_log.size(), &length, &info_log[0]);
std::cerr << "Info log: " << std::string(info_log.begin(), info_log.begin() + length);
glDeleteShader(shader);
throw std::runtime_error("Failed to compile shader.");
}
}
GLProgram::GLProgram( std::initializer_list< GLShader const * > shaders ) {
program = glCreateProgram();
for (auto s : shaders) {
glAttachShader(program, s->shader);
}
glLinkProgram(program);
GLint link_status = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &link_status);
if (link_status != GL_TRUE) {
std::cerr << "Failed to link shader program." << std::endl;
GLint info_log_length = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &info_log_length);
std::vector< GLchar > info_log(info_log_length, 0);
GLsizei length = 0;
glGetProgramInfoLog(program, info_log.size(), &length, &info_log[0]);
std::cerr << "Info log: " << std::string(info_log.begin(), info_log.begin() + length);
throw std::runtime_error("Failed to link program");
}
}
void GLProgram::DEBUG_dump_info(std::string const &name) {
std::cout << " ------ Shader Program '" << name << "' -----\n";
{
GLint active = 0;
glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &active);
std::cout << " " << active << " active attributes:\n";
for (GLuint l = 0; l < (GLuint)active; ++l) {
GLchar name[100];
GLint size = 0;
GLenum type = 0;
glGetActiveAttrib(program, l, 100, NULL, &size, &type, name);
name[99] = '\0';
GLuint idx = glGetAttribLocation(program, name);
auto str = [](GLenum type) -> std::string {
#undef DO
#define DO( X ) if (type == GL_ ## X) return #X;
DO( FLOAT ) DO( FLOAT_VEC2 ) DO( FLOAT_VEC3 ) DO( FLOAT_VEC4 )
DO( FLOAT_MAT2 ) DO( FLOAT_MAT2x3 ) DO( FLOAT_MAT2x4 )
DO( FLOAT_MAT3x2 ) DO( FLOAT_MAT3 ) DO( FLOAT_MAT3x4 )
DO( FLOAT_MAT4x2 ) DO( FLOAT_MAT4x3 ) DO( FLOAT_MAT4 )
DO( INT ) DO( INT_VEC2 ) DO( INT_VEC3 ) DO( INT_VEC4 )
DO( UNSIGNED_INT ) DO( UNSIGNED_INT_VEC2 ) DO( UNSIGNED_INT_VEC3 ) DO( UNSIGNED_INT_VEC4 )
DO( DOUBLE ) DO( DOUBLE_VEC2 ) DO( DOUBLE_VEC3 ) DO( DOUBLE_VEC4 )
DO( DOUBLE_MAT2 ) DO( DOUBLE_MAT2x3 ) DO( DOUBLE_MAT2x4 )
DO( DOUBLE_MAT3x2 ) DO( DOUBLE_MAT3 ) DO( DOUBLE_MAT3x4 )
DO( DOUBLE_MAT4x2 ) DO( DOUBLE_MAT4x3 ) DO( DOUBLE_MAT4 )
#undef DO
return "[unknown type: " + std::to_string(type);
};
std::cout << " (" << idx << ") " << name << " [" << str(type) << " x " << size << "]\n";
}
}
{
GLint active = 0;
glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &active);
std::cout << " " << active << " active uniforms:\n";
for (GLuint l = 0; l < (GLuint)active; ++l) {
GLchar name[100];
GLint size = 0;
GLenum type = 0;
glGetActiveUniform(program, l, 100, NULL, &size, &type, name);
name[99] = '\0';
GLuint idx = glGetUniformLocation(program, name);
auto str = [](GLenum type) -> std::string {
#define DO( X ) if (type == GL_ ## X) return #X;
DO( FLOAT ) DO( FLOAT_VEC2 ) DO( FLOAT_VEC3 ) DO( FLOAT_VEC4 )
DO( DOUBLE ) DO( DOUBLE_VEC2 ) DO( DOUBLE_VEC3 ) DO( DOUBLE_VEC4 )
DO( INT ) DO( INT_VEC2 ) DO( INT_VEC3 ) DO( INT_VEC4 )
DO( UNSIGNED_INT ) DO( UNSIGNED_INT_VEC2 ) DO( UNSIGNED_INT_VEC3 ) DO( UNSIGNED_INT_VEC4 )
DO( BOOL ) DO( BOOL_VEC2 ) DO( BOOL_VEC3 ) DO( BOOL_VEC4 )
DO( FLOAT_MAT2 ) DO( FLOAT_MAT2x3 ) DO( FLOAT_MAT2x4 )
DO( FLOAT_MAT3x2 ) DO( FLOAT_MAT3 ) DO( FLOAT_MAT3x4 )
DO( FLOAT_MAT4x2 ) DO( FLOAT_MAT4x3 ) DO( FLOAT_MAT4 )
DO( DOUBLE_MAT2 ) DO( DOUBLE_MAT2x3 ) DO( DOUBLE_MAT2x4 )
DO( DOUBLE_MAT3x2 ) DO( DOUBLE_MAT3 ) DO( DOUBLE_MAT3x4 )
DO( DOUBLE_MAT4x2 ) DO( DOUBLE_MAT4x3 ) DO( DOUBLE_MAT4 )
DO( SAMPLER_1D ) DO( SAMPLER_2D ) DO( SAMPLER_3D ) DO( SAMPLER_CUBE )
DO( SAMPLER_1D_SHADOW ) DO( SAMPLER_2D_SHADOW )
DO( SAMPLER_1D_ARRAY ) DO( SAMPLER_2D_ARRAY )
DO( SAMPLER_1D_ARRAY_SHADOW ) DO( SAMPLER_2D_ARRAY_SHADOW )
DO( SAMPLER_2D_MULTISAMPLE ) DO( SAMPLER_2D_MULTISAMPLE_ARRAY )
DO( SAMPLER_CUBE_SHADOW )
DO( SAMPLER_BUFFER )
DO( SAMPLER_2D_RECT ) DO( SAMPLER_2D_RECT_SHADOW )
DO( INT_SAMPLER_1D ) DO( INT_SAMPLER_2D ) DO( INT_SAMPLER_3D ) DO( INT_SAMPLER_CUBE )
DO( INT_SAMPLER_1D_ARRAY ) DO( INT_SAMPLER_2D_ARRAY )
DO( INT_SAMPLER_2D_MULTISAMPLE ) DO( INT_SAMPLER_2D_MULTISAMPLE_ARRAY )
DO( INT_SAMPLER_BUFFER ) DO( INT_SAMPLER_2D_RECT )
DO( UNSIGNED_INT_SAMPLER_1D ) DO( UNSIGNED_INT_SAMPLER_2D ) DO( UNSIGNED_INT_SAMPLER_3D ) DO( UNSIGNED_INT_SAMPLER_CUBE )
DO( UNSIGNED_INT_SAMPLER_1D_ARRAY ) DO( UNSIGNED_INT_SAMPLER_2D_ARRAY )
DO( UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE ) DO( UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE_ARRAY )
DO( UNSIGNED_INT_SAMPLER_BUFFER ) DO( UNSIGNED_INT_SAMPLER_2D_RECT )
DO( IMAGE_1D ) DO( IMAGE_2D ) DO( IMAGE_3D )
DO( IMAGE_2D_RECT ) DO( IMAGE_CUBE ) DO( IMAGE_BUFFER )
DO( IMAGE_1D_ARRAY ) DO( IMAGE_2D_ARRAY )
DO( IMAGE_2D_MULTISAMPLE ) DO( IMAGE_2D_MULTISAMPLE_ARRAY )
DO( INT_IMAGE_1D ) DO( INT_IMAGE_2D ) DO( INT_IMAGE_3D )
DO( INT_IMAGE_2D_RECT ) DO( INT_IMAGE_CUBE ) DO( INT_IMAGE_BUFFER )
DO( INT_IMAGE_1D_ARRAY ) DO( INT_IMAGE_2D_ARRAY )
DO( INT_IMAGE_2D_MULTISAMPLE ) DO( INT_IMAGE_2D_MULTISAMPLE_ARRAY )
DO( UNSIGNED_INT_IMAGE_1D ) DO( UNSIGNED_INT_IMAGE_2D ) DO( UNSIGNED_INT_IMAGE_3D )
DO( UNSIGNED_INT_IMAGE_2D_RECT ) DO( UNSIGNED_INT_IMAGE_CUBE ) DO( UNSIGNED_INT_IMAGE_BUFFER )
DO( UNSIGNED_INT_IMAGE_1D_ARRAY ) DO( UNSIGNED_INT_IMAGE_2D_ARRAY )
DO( UNSIGNED_INT_IMAGE_2D_MULTISAMPLE ) DO( UNSIGNED_INT_IMAGE_2D_MULTISAMPLE_ARRAY )
DO( UNSIGNED_INT_ATOMIC_COUNTER )
#undef DO
return "[unknown type: " + std::to_string(type);
};
std::cout << " (" << idx << ") " << name << " [" << str(type) << " x " << size << "]\n";
}
}
std::cout << " -------------------------------------\n";
}