-
Notifications
You must be signed in to change notification settings - Fork 6
/
GLShaderProgram.cpp
401 lines (340 loc) · 9.19 KB
/
GLShaderProgram.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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include "include/GLShaderProgram.h"
#include <boost/scoped_array.hpp>
#include <iostream>
namespace glp {
void ShaderProgram::check_shader_compile_status(GLuint obj)
{
GLint status;
glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
if(status == GL_FALSE)
{
GLint length;
glGetShaderiv(obj, GL_INFO_LOG_LENGTH, &length);
boost::scoped_array<char> log(new char[length]);
glGetShaderInfoLog(obj, length, &length, log.get());
GLSL_shader_exception e(log.get());
throw(e);
}
}
void ShaderProgram::check_program_link_status(GLuint obj)
{
GLint status;
glGetProgramiv(obj, GL_LINK_STATUS, &status);
if(status == GL_FALSE)
{
GLint length;
glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &length);
boost::scoped_array<char> log(new char[length]);
glGetProgramInfoLog(obj, length, &length, log.get());
GLSL_shader_exception e(log.get());
throw(e);
}
}
void ShaderProgram::setGeometryShaderSource(const std::string &source)
{
geometry_source = source;
has_geometry_shader = true;
}
void ShaderProgram::setVertexShaderSource(const std::string &source)
{
vertex_source = source;
has_vertex_shader = true;
}
void ShaderProgram::setFragmentShaderSource(const std::string &source)
{
fragment_source = source;
has_fragment_shader = true;
}
void ShaderProgram::compileProgram()
{
const char *source;
int length;
if(has_vertex_shader)
{
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
source = vertex_source.c_str();
length = vertex_source.size();
glShaderSource(vertex_shader, 1, &source, &length);
glCompileShader(vertex_shader);
check_shader_compile_status(vertex_shader);
}
if(has_geometry_shader)
{
geometry_shader = glCreateShader(GL_GEOMETRY_SHADER);
source = geometry_source.c_str();
length = geometry_source.size();
glShaderSource(geometry_shader, 1, &source, &length);
glCompileShader(geometry_shader);
check_shader_compile_status(geometry_shader);
}
if(has_fragment_shader)
{
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
source = fragment_source.c_str();
length = fragment_source.size();
glShaderSource(fragment_shader, 1, &source, &length);
glCompileShader(fragment_shader);
check_shader_compile_status(fragment_shader);
}
shader_program = glCreateProgram();
if(has_geometry_shader)
glAttachShader(shader_program, geometry_shader);
if(has_vertex_shader)
glAttachShader(shader_program, vertex_shader);
if(has_fragment_shader)
glAttachShader(shader_program, fragment_shader);
compiled = true;
}
void ShaderProgram::linkProgram()
{
glLinkProgram(shader_program);
check_program_link_status(shader_program);
linked = true;
}
void ShaderProgram::setTransformFeedbackVaryings(std::vector<const char*> varyings)
{
glTransformFeedbackVaryings(shader_program, varyings.size(), varyings.data(), GL_INTERLEAVED_ATTRIBS);
}
void ShaderProgram::bindAttributeLocation(const GLuint id, const std::string &name)
{
glBindAttribLocation(shader_program, id, name.c_str());
}
void ShaderProgram::bindFragDataLocation(const GLuint id, const std::string &name)
{
glBindFragDataLocation(shader_program, id, name.c_str());
}
void ShaderProgram::bindProgram()
{
glUseProgram(shader_program);
}
void ShaderProgram::unbindProgram()
{
glUseProgram(0);
}
void ShaderProgram::deleteProgram()
{
if(compiled)
{
unbindProgram();
if(has_vertex_shader)
glDetachShader(shader_program, vertex_shader);
if(has_geometry_shader)
glDetachShader(shader_program, geometry_shader);
if(has_fragment_shader)
glDetachShader(shader_program, fragment_shader);
if(has_vertex_shader)
glDeleteShader(vertex_shader);
if(has_geometry_shader)
glDeleteShader(geometry_shader);
if(has_fragment_shader)
glDeleteShader(fragment_shader);
glDeleteProgram(shader_program);
compiled = false;
linked = false;
}
}
ShaderProgram::~ShaderProgram()
{
deleteProgram();
}
GLint ShaderProgram::getUniformLocation(const std::string &name)
{
return glGetUniformLocation(shader_program, name.c_str());
}
void ShaderProgram::setUniform(GLint location, GLint val1)
{
glUniform1i(location, val1);
}
void ShaderProgram::setUniform(GLint location, GLint val1, GLint val2)
{
glUniform2i(location, val1, val2);
}
void ShaderProgram::setUniform(GLint location, GLint val1, GLint val2, GLint val3)
{
glUniform3i(location, val1, val2, val3);
}
void ShaderProgram::setUniform(GLint location, GLint val1, GLint val2, GLint val3, GLint val4)
{
glUniform4i(location, val1, val2, val3, val4);
}
void ShaderProgram::setUniform(GLint location, GLfloat val1)
{
glUniform1f(location, val1);
}
void ShaderProgram::setUniform(GLint location, GLfloat val1, GLfloat val2)
{
glUniform2f(location, val1, val2);
}
void ShaderProgram::setUniform(GLint location, GLfloat val1, GLfloat val2, GLfloat val3)
{
glUniform3f(location, val1, val2, val3);
}
void ShaderProgram::setUniform(GLint location, GLfloat val1, GLfloat val2, GLfloat val3, GLfloat val4)
{
glUniform4f(location, val1, val2, val3, val4);
}
void ShaderProgram::setUniform(GLint location, int size, GLint *val, int count)
{
switch(size)
{
case 1:
glUniform1iv(location, count, val);
return;
case 2:
glUniform2iv(location, count, val);
return;
case 3:
glUniform3iv(location, count, val);
return;
case 4:
glUniform4iv(location, count, val);
return;
default: return;
}
}
void ShaderProgram::setUniform(GLint location, int size, GLfloat *val, int count)
{
switch(size)
{
case 1:
glUniform1fv(location, count, val);
return;
case 2:
glUniform2fv(location, count, val);
return;
case 3:
glUniform3fv(location, count, val);
return;
case 4:
glUniform4fv(location, count, val);
return;
default: return;
}
}
void ShaderProgram::setUniformMatrix(GLint location, int width, int height, GLfloat *val, int count)
{
switch(width)
{
case 2:
switch(height)
{
case 2:
glUniformMatrix2fv(location, count, false, val);
return;
case 3:
glUniformMatrix2x3fv(location, count, false, val);
return;
case 4:
glUniformMatrix2x4fv(location, count, false, val);
return;
default: return;
}
case 3:
switch(height)
{
case 2:
glUniformMatrix3x2fv(location, count, false, val);
return;
case 3:
glUniformMatrix3fv(location, count, false, val);
return;
case 4:
glUniformMatrix3x4fv(location, count, false, val);
return;
default: return;
}
case 4:
switch(height)
{
case 2:
glUniformMatrix4x2fv(location, count, false, val);
return;
case 3:
glUniformMatrix4x3fv(location, count, false, val);
return;
case 4:
glUniformMatrix4fv(location, count, false, val);
return;
default: return;
}
default: return;
}
}
void ShaderProgram::setUniform(const std::string &name, GLint val1)
{
setUniform(getUniformLocation(name), val1);
}
void ShaderProgram::setUniform(const std::string &name, GLint val1, GLint val2)
{
setUniform(getUniformLocation(name), val1, val2);
}
void ShaderProgram::setUniform(const std::string &name, GLint val1, GLint val2, GLint val3)
{
setUniform(getUniformLocation(name), val1, val2, val3);
}
void ShaderProgram::setUniform(const std::string &name, GLint val1, GLint val2, GLint val3, GLint val4)
{
setUniform(getUniformLocation(name), val1, val2, val3, val4);
}
void ShaderProgram::setUniform(const std::string &name, GLfloat val1)
{
setUniform(getUniformLocation(name), val1);
}
void ShaderProgram::setUniform(const std::string &name, GLfloat val1, GLfloat val2)
{
setUniform(getUniformLocation(name), val1, val2);
}
void ShaderProgram::setUniform(const std::string &name, GLfloat val1, GLfloat val2, GLfloat val3)
{
setUniform(getUniformLocation(name), val1, val2, val3);
}
void ShaderProgram::setUniform(const std::string &name, GLfloat val1, GLfloat val2, GLfloat val3, GLfloat val4)
{
setUniform(getUniformLocation(name), val1, val2, val3, val4);
}
void ShaderProgram::setUniform(const std::string &name, int size, GLint *val, int count)
{
setUniform(getUniformLocation(name), size, val, count);
}
void ShaderProgram::setUniform(const std::string &name, int size, GLfloat *val, int count)
{
setUniform(getUniformLocation(name), size, val, count);
}
void ShaderProgram::setUniformMatrix(const std::string &name, int width, int height, GLfloat *val, int count)
{
setUniformMatrix(getUniformLocation(name), width, height, val, count);
}
std::vector<unsigned char> ShaderProgram::getBinary()
{
if(!linked)
return std::vector<unsigned char>();
GLint length1, length2;
glGetProgramiv(shader_program, GL_PROGRAM_BINARY_LENGTH, &length1);
std::vector<unsigned char> binary(sizeof(GLenum)+length1);
glGetProgramBinary(shader_program, length1, &length2,
reinterpret_cast<GLenum*>(binary.data()),
binary.data()+sizeof(GLenum));
return binary;
}
void ShaderProgram::loadBinary(std::vector<unsigned char> &binary)
{
if(compiled)
{
deleteProgram();
vertex_source.clear();
geometry_source.clear();
fragment_source.clear();
has_fragment_shader = false;
has_vertex_shader = false;
has_geometry_shader = false;
}
shader_program = glCreateProgram();
glProgramBinary(shader_program,
*reinterpret_cast<GLenum*>(binary.data()),
binary.data()+sizeof(GLenum),
binary.size()-sizeof(GLenum));
check_program_link_status(shader_program);
compiled = true;
linked = true;
}
}