-
Notifications
You must be signed in to change notification settings - Fork 0
/
shader.cpp
201 lines (153 loc) · 5.49 KB
/
shader.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
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
#include <string>
#include <vector>
#include <fstream>
#include <glog/logging.h>
#include <glm/gtc/type_ptr.hpp>
#include "shader.hpp"
// #include "simple_file.hpp"
using namespace gl;
// using namespace simple_file;
void compile_info(const GLuint shader);
void link_info(const GLuint shader);
void load_shader(const GLuint shader, std::string fn);
void variable_info(const GLuint program);
std::string error_name ("<not loaded>");
shader::shader(std::string vs_fn, std::string fs_fn, std::vector<std::string> fbv)
:
vertex_shader_name_(vs_fn),
fragment_shader_name_(fs_fn),
feedback_varying_names_(fbv)
{
reload();
}
shader::shader(std::string filename, std::vector<std::string> fbv): shader(filename, filename, fbv) {}
shader::shader(std::string filename): shader(filename, filename, {}) {}
shader::shader(std::string vs_fn, std::string fs_fn): shader(vs_fn, fs_fn, {}) {}
shader::shader(std::pair<std::string, std::string> names): shader(names.first, names.second, {}) {}
shader::~shader() { glDeleteProgram(_program); }
void shader::use() { glUseProgram(_program); }
void shader::u1f(std::string name, float value) {
use();
GLint u = glGetUniformLocation(_program, name.c_str());
glUniform1f(u, value);
}
void shader::u2f(std::string name, glm::vec2 value) {
use();
GLint u = glGetUniformLocation(_program, name.c_str());
glUniform2f(u, value.x, value.y);
}
void shader::u3f(std::string name, glm::vec3 value) {
use();
GLint u = glGetUniformLocation(_program, name.c_str());
glUniform3f(u, value.x, value.y, value.z);
}
void shader::u4f(std::string name, glm::vec4 value) {
use();
GLint u = glGetUniformLocation(_program, name.c_str());
glUniform4f(u, value.x, value.y, value.z, value.w);
}
void shader::u1i(std::string name, GLint value) {
use();
GLint u = glGetUniformLocation(_program, name.c_str());
glUniform1i(u, value);
}
void shader::u44m(std::string name, glm::mat4 m) {
use();
GLint u = glGetUniformLocation(_program, name.c_str());
glUniformMatrix4fv(u, 1, GL_FALSE, glm::value_ptr(m));
}
std::string shader::dir;
void shader::setdir(std::string d) {
dir = d;
}
void shader::reload() {
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
LOG(INFO) << "loading: " << vertex_shader_name_;
load_shader(vs, dir + vertex_shader_name_ + ".vs.glsl");
LOG(INFO) << "loading: " << fragment_shader_name_;
load_shader(fs, dir + fragment_shader_name_ + ".fs.glsl");
glCompileShader(vs);
compile_info(vs);
glCompileShader(fs);
compile_info(fs);
_program = glCreateProgram();
glAttachShader(_program, vs);
glAttachShader(_program, fs);
if(feedback_varying_names_.size()) {
std::vector<const GLchar*> varyings;
for (auto i = feedback_varying_names_.begin(); i != feedback_varying_names_.end(); ++i) {
varyings.push_back(i->c_str());
}
glTransformFeedbackVaryings(_program, varyings.size(), &varyings[0], GL_INTERLEAVED_ATTRIBS);
}
glLinkProgram(_program);
link_info(_program);
variable_info(_program);
}
void compile_info(const GLuint shader) {
GLint status(0);
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
GLint maxLength(0);
GLint logLength(0);
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
GLchar *log = new GLchar[maxLength];
glGetShaderInfoLog(shader, maxLength, &logLength, log);
if (1 != status) {
LOG(ERROR) << "glsl compile error: " << log;
} else if (maxLength) {
LOG(INFO) << log;
}
}
void link_info(const GLuint program) {
GLint status(0);
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (1 != status) {
GLint maxLength(0);
GLint logLength(0);
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);
GLchar *log = new GLchar[maxLength];
glGetProgramInfoLog(program, maxLength, &logLength, log);
LOG(ERROR) << "glsl link error: " << log;
}
}
void load_shader(const GLuint shader, std::string fn) {
std::ifstream srcfile(fn, std::ios::in | std::ios::binary);
if (!srcfile.is_open()) {
LOG(FATAL) << "couldn't open: " << fn;
}
srcfile.seekg(0, std::ios::end);
int sz = srcfile.tellg();
char *buf = new char[sz + 1];
srcfile.seekg(0, std::ios::beg);
srcfile.read(buf, sz);
buf[sz] = '\0'; // ensure null terminator
glShaderSource(shader, 1, &buf, NULL);
delete[] buf;
}
void variable_info(const GLuint program) {
GLint n_active_attr = 0;
GLint n_active_ufrm = 0;
GLint max_attr_namelength = 0;
GLint max_ufrm_namelength = 0;
glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &n_active_attr);
glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &n_active_ufrm);
glGetProgramiv(program, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &max_attr_namelength);
glGetProgramiv(program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &max_ufrm_namelength);
std::vector<GLchar> nameData(max_attr_namelength);
for (int attr = 0; attr < n_active_attr; ++attr) {
GLint arraySize = 0;
GLenum type = GL_FLOAT;
GLsizei actualLength = 0;
glGetActiveAttrib(program, attr, nameData.size(), &actualLength, &arraySize, &type, &nameData[0]);
LOG(INFO) << "attr " << attr << ": " << std::string((char *)&nameData[0], actualLength);
}
nameData = std::vector<GLchar>(max_ufrm_namelength);
for (int ufrm = 0; ufrm < n_active_ufrm; ++ufrm) {
GLint arraySize = 0;
GLenum type = GL_FLOAT;
GLsizei actualLength = 0;
glGetActiveUniform(program, ufrm, nameData.size(), &actualLength, &arraySize, &type, &nameData[0]);
LOG(INFO) << "ufrm " << ufrm << ": " << std::string((char *)&nameData[0], actualLength);
}
}