-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cpp
465 lines (382 loc) · 15.1 KB
/
Program.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#include "Program.h"
#include <fstream>
using std::ifstream;
using std::ios;
#include <sstream>
#include <sys/stat.h>
namespace windmap {
namespace GLSLShaderInfo {
struct shader_file_extension {
const char *ext;
GLSLShader::GLSLShaderType type;
};
struct shader_file_extension extensions[] =
{
{".vs", GLSLShader::VERTEX},
{".vert", GLSLShader::VERTEX},
{".gs", GLSLShader::GEOMETRY},
{".geom", GLSLShader::GEOMETRY},
{".tcs", GLSLShader::TESS_CONTROL},
{".tes", GLSLShader::TESS_EVALUATION},
{".fs", GLSLShader::FRAGMENT},
{".frag", GLSLShader::FRAGMENT},
{".cs", GLSLShader::COMPUTE}
};
}
GLSLProgram::GLSLProgram() : handle(0), linked(false) { }
GLSLProgram::~GLSLProgram() {
if(handle == 0) return;
// Query the number of attached shaders
GLint numShaders = 0;
glGetProgramiv(handle, GL_ATTACHED_SHADERS, &numShaders);
// Get the shader names
GLuint * shaderNames = new GLuint[numShaders];
glGetAttachedShaders(handle, numShaders, NULL, shaderNames);
// Delete the shaders
for (int i = 0; i < numShaders; i++)
glDeleteShader(shaderNames[i]);
// Delete the program
glDeleteProgram (handle);
delete[] shaderNames;
}
void GLSLProgram::compileShader( const char * fileName )
throw( GLSLProgramException ) {
int numExts = sizeof(GLSLShaderInfo::extensions) / sizeof(GLSLShaderInfo::shader_file_extension);
// Check the file name's extension to determine the shader type
string ext = getExtension( fileName );
GLSLShader::GLSLShaderType type = GLSLShader::VERTEX;
bool matchFound = false;
for( int i = 0; i < numExts; i++ ) {
if( ext == GLSLShaderInfo::extensions[i].ext ) {
matchFound = true;
type = GLSLShaderInfo::extensions[i].type;
break;
}
}
// If we didn't find a match, throw an exception
if( !matchFound ) {
string msg = "Unrecognized extension: " + ext;
throw GLSLProgramException(msg);
}
// Pass the discovered shader type along
compileShader( fileName, type );
}
string GLSLProgram::getExtension( const char * name ) {
string nameStr(name);
size_t loc = nameStr.find_last_of('.');
if( loc != string::npos ) {
return nameStr.substr(loc, string::npos);
}
return "";
}
void GLSLProgram::compileShader( const char * fileName,
GLSLShader::GLSLShaderType type )
throw( GLSLProgramException )
{
if( ! fileExists(fileName) )
{
string message = string("Shader: ") + fileName + " not found.";
throw GLSLProgramException(message);
}
if( handle <= 0 ) {
handle = glCreateProgram();
if( handle == 0) {
throw GLSLProgramException("Unable to create shader program.");
}
}
ifstream inFile( fileName, ios::in );
if( !inFile ) {
string message = string("Unable to open: ") + fileName;
throw GLSLProgramException(message);
}
// Get file contents
std::stringstream code;
code << inFile.rdbuf();
inFile.close();
compileShader(code.str(), type, fileName);
}
void GLSLProgram::compileShader( const string & source,
GLSLShader::GLSLShaderType type,
const char * fileName )
throw(GLSLProgramException)
{
if( handle <= 0 ) {
handle = glCreateProgram();
if( handle == 0) {
throw GLSLProgramException("Unable to create shader program.");
}
}
GLuint shaderHandle = glCreateShader(type);
const char * c_code = source.c_str();
glShaderSource( shaderHandle, 1, &c_code, NULL );
// Compile the shader
glCompileShader(shaderHandle);
// Check for errors
int result;
glGetShaderiv( shaderHandle, GL_COMPILE_STATUS, &result );
if( GL_FALSE == result ) {
// Compile failed, get log
int length = 0;
string logString;
glGetShaderiv(shaderHandle, GL_INFO_LOG_LENGTH, &length );
if( length > 0 ) {
char * c_log = new char[length];
int written = 0;
glGetShaderInfoLog(shaderHandle, length, &written, c_log);
logString = c_log;
delete [] c_log;
}
string msg;
if( fileName ) {
msg = string(fileName) + ": shader compliation failed\n";
} else {
msg = "Shader compilation failed.\n";
}
msg += logString;
throw GLSLProgramException(msg);
} else {
// Compile succeeded, attach shader
glAttachShader(handle, shaderHandle);
}
}
void GLSLProgram::link() throw(GLSLProgramException)
{
if( linked ) return;
if( handle <= 0 )
throw GLSLProgramException("Program has not been compiled.");
glLinkProgram(handle);
int status = 0;
glGetProgramiv( handle, GL_LINK_STATUS, &status);
if( GL_FALSE == status ) {
// Store log and return false
int length = 0;
string logString;
glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &length );
if( length > 0 ) {
char * c_log = new char[length];
int written = 0;
glGetProgramInfoLog(handle, length, &written, c_log);
logString = c_log;
delete [] c_log;
}
throw GLSLProgramException(string("Program link failed:\n") + logString);
} else {
uniformLocations.clear();
linked = true;
}
}
void GLSLProgram::bind() throw(GLSLProgramException)
{
if( handle <= 0 || (! linked) )
throw GLSLProgramException("Shader has not been linked");
glUseProgram( handle );
}
void GLSLProgram::unbind()
{
glUseProgram(0);
}
int GLSLProgram::getHandle()
{
return handle;
}
bool GLSLProgram::isLinked()
{
return linked;
}
void GLSLProgram::bindAttribLocation( GLuint location, const char * name)
{
glBindAttribLocation(handle, location, name);
}
void GLSLProgram::bindFragDataLocation( GLuint location, const char * name )
{
glBindFragDataLocation(handle, location, name);
}
void GLSLProgram::setUniform( const char *name, float x, float y, float z)
{
GLint loc = getUniformLocation(name);
glUniform3f(loc,x,y,z);
}
void GLSLProgram::setUniform( const char *name, const vec3 & v)
{
this->setUniform(name,v.x,v.y,v.z);
}
void GLSLProgram::setUniform( const char *name, const vec4 & v)
{
GLint loc = getUniformLocation(name);
glUniform4f(loc,v.x,v.y,v.z,v.w);
}
void GLSLProgram::setUniform( const char *name, const vec2 & v)
{
GLint loc = getUniformLocation(name);
glUniform2f(loc,v.x,v.y);
}
void GLSLProgram::setUniform( const char *name, const mat4 & m)
{
GLint loc = getUniformLocation(name);
glUniformMatrix4fv(loc, 1, GL_FALSE, &m[0][0]);
}
void GLSLProgram::setUniform( const char *name, const mat3 & m)
{
GLint loc = getUniformLocation(name);
glUniformMatrix3fv(loc, 1, GL_FALSE, &m[0][0]);
}
void GLSLProgram::setUniform( const char *name, const float m[16])
{
GLint loc = getUniformLocation(name);
glUniformMatrix4fv(loc, 1, GL_FALSE, m);
}
void GLSLProgram::setUniform( const char *name, float val )
{
GLint loc = getUniformLocation(name);
glUniform1f(loc, val);
}
void GLSLProgram::setUniform( const char *name, int val )
{
GLint loc = getUniformLocation(name);
glUniform1i(loc, val);
}
void GLSLProgram::setUniform( const char *name, GLuint val )
{
GLint loc = getUniformLocation(name);
glUniform1ui(loc, val);
}
void GLSLProgram::setUniform( const char *name, bool val )
{
int loc = getUniformLocation(name);
glUniform1i(loc, val);
}
void GLSLProgram::printActiveUniforms() {
#ifndef OMEGALIB_MODULE
GLint numUniforms = 0;
glGetProgramInterfaceiv( handle, GL_UNIFORM, GL_ACTIVE_RESOURCES, &numUniforms);
GLenum properties[] = {GL_NAME_LENGTH, GL_TYPE, GL_LOCATION, GL_BLOCK_INDEX};
printf("Active uniforms:\n");
for( int i = 0; i < numUniforms; ++i ) {
GLint results[4];
glGetProgramResourceiv(handle, GL_UNIFORM, i, 4, properties, 4, NULL, results);
if( results[3] != -1 ) continue; // Skip uniforms in blocks
GLint nameBufSize = results[0] + 1;
char * name = new char[nameBufSize];
glGetProgramResourceName(handle, GL_UNIFORM, i, nameBufSize, NULL, name);
printf("%-5d %s (%s)\n", results[2], name, getTypeString(results[1]));
delete [] name;
}
#endif
}
void GLSLProgram::printActiveUniformBlocks() {
#ifndef OMEGALIB_MODULE
GLint numBlocks = 0;
glGetProgramInterfaceiv(handle, GL_UNIFORM_BLOCK, GL_ACTIVE_RESOURCES, &numBlocks);
GLenum blockProps[] = {GL_NUM_ACTIVE_VARIABLES, GL_NAME_LENGTH};
GLenum blockIndex[] = {GL_ACTIVE_VARIABLES};
GLenum props[] = {GL_NAME_LENGTH, GL_TYPE, GL_BLOCK_INDEX};
for(int block = 0; block < numBlocks; ++block) {
GLint blockInfo[2];
glGetProgramResourceiv(handle, GL_UNIFORM_BLOCK, block, 2, blockProps, 2, NULL, blockInfo);
GLint numUnis = blockInfo[0];
char * blockName = new char[blockInfo[1]+1];
glGetProgramResourceName(handle, GL_UNIFORM_BLOCK, block, blockInfo[1]+1, NULL, blockName);
printf("Uniform block \"%s\":\n", blockName);
delete [] blockName;
GLint * unifIndexes = new GLint[numUnis];
glGetProgramResourceiv(handle, GL_UNIFORM_BLOCK, block, 1, blockIndex, numUnis, NULL, unifIndexes);
for( int unif = 0; unif < numUnis; ++unif ) {
GLint uniIndex = unifIndexes[unif];
GLint results[3];
glGetProgramResourceiv(handle, GL_UNIFORM, uniIndex, 3, props, 3, NULL, results);
GLint nameBufSize = results[0] + 1;
char * name = new char[nameBufSize];
glGetProgramResourceName(handle, GL_UNIFORM, uniIndex, nameBufSize, NULL, name);
printf(" %s (%s)\n", name, getTypeString(results[1]));
delete [] name;
}
delete [] unifIndexes;
}
#endif
}
void GLSLProgram::printActiveAttribs() {
#ifndef OMEGALIB_MODULE
GLint numAttribs;
glGetProgramInterfaceiv( handle, GL_PROGRAM_INPUT, GL_ACTIVE_RESOURCES, &numAttribs);
GLenum properties[] = {GL_NAME_LENGTH, GL_TYPE, GL_LOCATION};
printf("Active attributes:\n");
for( int i = 0; i < numAttribs; ++i ) {
GLint results[3];
glGetProgramResourceiv(handle, GL_PROGRAM_INPUT, i, 3, properties, 3, NULL, results);
GLint nameBufSize = results[0] + 1;
char * name = new char[nameBufSize];
glGetProgramResourceName(handle, GL_PROGRAM_INPUT, i, nameBufSize, NULL, name);
printf("%-5d %s (%s)\n", results[2], name, getTypeString(results[1]));
delete [] name;
}
#endif
}
const char * GLSLProgram::getTypeString( GLenum type ) {
// There are many more types than are covered here, but
// these are the most common in these examples.
switch(type) {
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_DOUBLE:
return "double";
case GL_INT:
return "int";
case GL_UNSIGNED_INT:
return "unsigned int";
case GL_BOOL:
return "bool";
case GL_FLOAT_MAT2:
return "mat2";
case GL_FLOAT_MAT3:
return "mat3";
case GL_FLOAT_MAT4:
return "mat4";
default:
return "?";
}
}
void GLSLProgram::validate() throw(GLSLProgramException)
{
if( ! isLinked() )
throw GLSLProgramException("Program is not linked");
GLint status;
glValidateProgram( handle );
glGetProgramiv( handle, GL_VALIDATE_STATUS, &status );
if( GL_FALSE == status ) {
// Store log and return false
int length = 0;
string logString;
glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &length );
if( length > 0 ) {
char * c_log = new char[length];
int written = 0;
glGetProgramInfoLog(handle, length, &written, c_log);
logString = c_log;
delete [] c_log;
}
throw GLSLProgramException(string("Program failed to validate\n") + logString);
}
}
int GLSLProgram::getUniformLocation(const char * name )
{
std::map<string, int>::iterator pos;
pos = uniformLocations.find(name);
if( pos == uniformLocations.end() ) {
uniformLocations[name] = glGetUniformLocation(handle, name);
}
return uniformLocations[name];
}
bool GLSLProgram::fileExists( const string & fileName )
{
struct stat info;
int ret = -1;
ret = stat(fileName.c_str(), &info);
return 0 == ret;
}
}; //namespace windmap