-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram.cc
245 lines (200 loc) · 7.93 KB
/
program.cc
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
#include "program.hh"
#include <glm/gtc/type_ptr.hpp>
#include <fstream>
#include <sstream>
#include "debug.hh" // order matters, read debug.hh
program::program(const std::vector<shader>& shaders) :
globject(glCreateProgram())
{
GLint status; // captures program link status
std::string errmsg; // holds possible link errmsg
// check objects
check(globject == 0);
check(shaders.size() == 0);
// attach shaders
for (unsigned i = 0; i < shaders.size(); i++)
glAttachShader(globject, shaders[i].get());
// compose program
glLinkProgram(globject);
// detach shaders
for (unsigned i = 0; i < shaders.size(); i++)
glDetachShader(globject, shaders[i].get());
// check for link errors
glGetProgramiv(globject, GL_LINK_STATUS, &status);
// throw if any exist
if (status == GL_FALSE) {
errmsg = error(globject);
glDeleteProgram(globject);
mcheck(1, "%s\n", errmsg.c_str());
}
}
program::~program()
{
check(globject == 0);
glDeleteProgram(globject);
}
/*
* Check if current proram is in use.
*/
bool program::in_use() const
{
GLint current = 0; // current program in use by the API
check(globject == 0);
glGetIntegerv(GL_CURRENT_PROGRAM, ¤t);
return current == static_cast<GLint>(globject);
}
/*
* Toggle usage for the current program.
*/
void program::toggle() const
{
if (in_use())
glUseProgram(0);
else
glUseProgram(globject);
}
/*
* Get resource attrib location via glGetAttribLocation.
*/
GLint program::attrib(const GLchar *name) const
{
GLint index; // holds attribute index
check(name == nullptr);
index = glGetAttribLocation(globject, name);
check(index == -1);
return index;
}
/*
* Get resource uniform location via glGetUniformLocation.
*/
GLint program::uniform(const GLchar *name) const
{
GLint index; // holds uniform index
check(name == nullptr);
index = glGetUniformLocation(globject, name);
check(index == -1);
return index;
}
/*
* Compose error message to throw (helper function).
*/
std::string program::error(GLuint globject)
{
char *log_str; // C style log info string
GLint log_len; // Info log total length
std::string errmsg; // Composed error msg
errmsg = std::string("Program linking failure: ");
glGetProgramiv(globject, GL_INFO_LOG_LENGTH, &log_len);
log_str = new char[log_len + 1];
glGetProgramInfoLog(globject, log_len, nullptr, log_str);
errmsg += log_str;
delete[] log_str;
return errmsg;
}
/*
* Write signatures as macros
*/
#define ATTRIB_N_UNIFORM_SETTERS(OGL_TYPE, TYPE_PREFIX, TYPE_SUFFIX) \
\
void program::setAttrib(const GLchar* name, OGL_TYPE v0) \
{ assert(in_use()); glVertexAttrib ## TYPE_PREFIX ## 1 ## TYPE_SUFFIX (attrib(name), v0); } \
void program::setAttrib(const GLchar* name, OGL_TYPE v0, OGL_TYPE v1) \
{ assert(in_use()); glVertexAttrib ## TYPE_PREFIX ## 2 ## TYPE_SUFFIX (attrib(name), v0, v1); } \
void program::setAttrib(const GLchar* name, OGL_TYPE v0, OGL_TYPE v1, OGL_TYPE v2) \
{ assert(in_use()); glVertexAttrib ## TYPE_PREFIX ## 3 ## TYPE_SUFFIX (attrib(name), v0, v1, v2); } \
void program::setAttrib(const GLchar* name, OGL_TYPE v0, OGL_TYPE v1, OGL_TYPE v2, OGL_TYPE v3) \
{ assert(in_use()); glVertexAttrib ## TYPE_PREFIX ## 4 ## TYPE_SUFFIX (attrib(name), v0, v1, v2, v3); } \
\
void program::setAttrib1v(const GLchar* name, const OGL_TYPE* v) \
{ assert(in_use()); glVertexAttrib ## TYPE_PREFIX ## 1 ## TYPE_SUFFIX ## v (attrib(name), v); } \
void program::setAttrib2v(const GLchar* name, const OGL_TYPE* v) \
{ assert(in_use()); glVertexAttrib ## TYPE_PREFIX ## 2 ## TYPE_SUFFIX ## v (attrib(name), v); } \
void program::setAttrib3v(const GLchar* name, const OGL_TYPE* v) \
{ assert(in_use()); glVertexAttrib ## TYPE_PREFIX ## 3 ## TYPE_SUFFIX ## v (attrib(name), v); } \
void program::setAttrib4v(const GLchar* name, const OGL_TYPE* v) \
{ assert(in_use()); glVertexAttrib ## TYPE_PREFIX ## 4 ## TYPE_SUFFIX ## v (attrib(name), v); } \
\
void program::setUniform(const GLchar* name, OGL_TYPE v0) \
{ assert(in_use()); glUniform1 ## TYPE_SUFFIX (uniform(name), v0); } \
void program::setUniform(const GLchar* name, OGL_TYPE v0, OGL_TYPE v1) \
{ assert(in_use()); glUniform2 ## TYPE_SUFFIX (uniform(name), v0, v1); } \
void program::setUniform(const GLchar* name, OGL_TYPE v0, OGL_TYPE v1, OGL_TYPE v2) \
{ assert(in_use()); glUniform3 ## TYPE_SUFFIX (uniform(name), v0, v1, v2); } \
void program::setUniform(const GLchar* name, OGL_TYPE v0, OGL_TYPE v1, OGL_TYPE v2, OGL_TYPE v3) \
{ assert(in_use()); glUniform4 ## TYPE_SUFFIX (uniform(name), v0, v1, v2, v3); } \
\
void program::setUniform1v(const GLchar* name, const OGL_TYPE* v, GLsizei count) \
{ assert(in_use()); glUniform1 ## TYPE_SUFFIX ## v (uniform(name), count, v); } \
void program::setUniform2v(const GLchar* name, const OGL_TYPE* v, GLsizei count) \
{ assert(in_use()); glUniform2 ## TYPE_SUFFIX ## v (uniform(name), count, v); } \
void program::setUniform3v(const GLchar* name, const OGL_TYPE* v, GLsizei count) \
{ assert(in_use()); glUniform3 ## TYPE_SUFFIX ## v (uniform(name), count, v); } \
void program::setUniform4v(const GLchar* name, const OGL_TYPE* v, GLsizei count) \
{ assert(in_use()); glUniform4 ## TYPE_SUFFIX ## v (uniform(name), count, v); }
/*
* Generate previously proposed declarations
*/
ATTRIB_N_UNIFORM_SETTERS(GLfloat, , f);
ATTRIB_N_UNIFORM_SETTERS(GLdouble, , d);
ATTRIB_N_UNIFORM_SETTERS(GLint, I, i);
ATTRIB_N_UNIFORM_SETTERS(GLuint, I, ui);
/*
* Uniforms & Attribs should be set through these
*/
void program::setUniformMatrix2(const GLchar *name, const GLfloat *v,
GLsizei count, GLboolean transpose)
{
check(!in_use());
glUniformMatrix2fv(uniform(name), count, transpose, v);
}
void program::setUniformMatrix3(const GLchar *name, const GLfloat *v,
GLsizei count, GLboolean transpose)
{
check(!in_use());
glUniformMatrix3fv(uniform(name), count, transpose, v);
}
void program::setUniformMatrix4(const GLchar *name, const GLfloat *v,
GLsizei count, GLboolean transpose)
{
check(!in_use());
glUniformMatrix4fv(uniform(name), count, transpose, v);
}
void program::setUniform(const GLchar *name, const glm::mat2& m,
GLboolean transpose)
{
check(!in_use());
glUniformMatrix2fv(uniform(name), 1, transpose, glm::value_ptr(m));
}
void program::setUniform(const GLchar *name, const glm::mat3& m,
GLboolean transpose)
{
check(!in_use());
glUniformMatrix3fv(uniform(name), 1, transpose, glm::value_ptr(m));
}
void program::setUniform(const GLchar *name, const glm::mat4& m,
GLboolean transpose)
{
check(!in_use());
glUniformMatrix4fv(uniform(name), 1, transpose, glm::value_ptr(m));
}
void program::setUniform(const GLchar *uniformName, const glm::vec3& v)
{
setUniform3v(uniformName, glm::value_ptr(v));
}
void program::setUniform(const GLchar *uniformName, const glm::vec4& v)
{
setUniform4v(uniformName, glm::value_ptr(v));
}
/*
* Instantiate shader from source file.
*/
shader shader_from_file(const std::string&& path, GLenum type)
{
std::ifstream handle(path, std::ios::in | std::ios::binary);
check(!handle.is_open());
// Assert the filepath
info("shader path is: %s\n", path.c_str());
std::stringstream buffer;
buffer << handle.rdbuf();
return shader(buffer.str(), type);
}