-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogram.cpp
More file actions
382 lines (314 loc) · 9.03 KB
/
program.cpp
File metadata and controls
382 lines (314 loc) · 9.03 KB
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
#include <GL/glew.h>
#include <GL/gl.h>
#include <assert.h>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <glm/gtc/matrix_transform.hpp>
#include "program.h"
using namespace std;
using namespace glm;
namespace EZGraphics {
/* ------------------------------------------- */
ShaderFile::ShaderFile ( ShaderType t, string n ) : name(n), type(t) {}
ShaderFile::ShaderFile() : name(""), type(Vert) {}
/* ------------------------------------------- */
const Program * Program::laston = NULL;
/* ------------------------------------------- */
Program::Program ( const Program &a )
{
cerr << "Attempting to copy Program object" << endl;
exit(1);
}
/* ------------------------------------------- */
Program::Program ( const Shader *aa, const Shader *bb, const Shader *cc, const Shader *dd, const Shader *ee ) :
c(NULL), e(NULL), v(NULL), f(NULL), g(NULL), one_warning_printed(false)
{
const Shader *p[5] = {aa,bb,cc,dd,ee};
handle = glCreateProgram();
for ( int i=0; i<5; i++ )
if (p[i])
{
switch(p[i]->getType())
{
case Vert :
v = p[i];
break;
case Frag:
f = p[i];
break;
case Geom:
g = p[i];
break;
case TessE:
e = p[i];
break;
case TessC:
c = p[i];
break;
default:
exit(1);
}
glAttachShader(handle,p[i]->getHandle());
}
glLinkProgram(handle);
for ( int i=0; i<5; i++ )
if (p[i])
glDetachShader(handle,p[i]->getHandle());
GLint n;
glGetProgramiv(handle,GL_ACTIVE_UNIFORMS,&n);
static GLchar vname[GL_ACTIVE_UNIFORM_MAX_LENGTH];
for ( int i=0; i<n; i++ )
{
GLsizei chars;
GLint size;
GLenum t;
glGetActiveUniform(handle,i,GL_ACTIVE_UNIFORM_MAX_LENGTH,&chars,&size,&t,vname);
univars[string(vname)] = unidata(glGetUniformLocation(handle,vname),t);
}
}
/* ------------------------------------------- */
Program::Program ( const vector <string> &l,
const Shader *aa, const Shader *bb, const Shader *cc,
const Shader *dd, const Shader *ee ) :
c(NULL), e(NULL), v(NULL), f(NULL), g(NULL), one_warning_printed(false)
{
const Shader *p[5] = {aa,bb,cc,dd,ee};
handle = glCreateProgram();
for ( int i=0; i<5; i++ )
if (p[i])
{
switch(p[i]->getType())
{
case Vert :
v = p[i];
break;
case Frag:
f = p[i];
break;
case Geom:
g = p[i];
break;
case TessE:
e = p[i];
break;
case TessC:
c = p[i];
break;
default:
exit(1);
}
glAttachShader(handle,p[i]->getHandle());
}
const char **names = new const char*[l.size()];
int index = 0;
for ( vector<string>::const_iterator i = l.begin(); i!=l.end(); ++i )
names[index++] = i->c_str();
glTransformFeedbackVaryings(handle,index,names,GL_SEPARATE_ATTRIBS);
glLinkProgram(handle);
delete[] names;
for ( int i=0; i<5; i++ )
if (p[i])
glDetachShader(handle,p[i]->getHandle());
GLint n;
glGetProgramiv(handle,GL_ACTIVE_UNIFORMS,&n);
static GLchar vname[GL_ACTIVE_UNIFORM_MAX_LENGTH];
for ( int i=0; i<n; i++ )
{
GLsizei chars;
GLint size;
GLenum t;
glGetActiveUniform(handle,i,GL_ACTIVE_UNIFORM_MAX_LENGTH,&chars,&size,&t,vname);
univars[string(vname)] = unidata(glGetUniformLocation(handle,vname),t);
}
}
/* ------------------------------------------- */
Program & Program::operator= ( const Program & rhs )
{
cerr << "Attempting assignment for a Program object" << endl;
exit(1);
}
/* ------------------------------------------- */
Program::~Program()
{
if (!handle)
return;
assert(glIsProgram(handle));
glDeleteProgram(handle);
}
/* ------------------------------------------- */
void Program::on() const
{
laston = this;
glUseProgram(handle);
}
/* ------------------------------------------- */
void Program::off() const
{
laston = NULL;
glUseProgram(0);
}
/* ------------------------------------------- */
#define SETUNIFORM(ITYPE,GLTYPE,GLSET,VACCESS) \
void Program::setUniform ( const GLchar *name, ITYPE v ) \
{ \
map<string,unidata>::iterator u = univars.find(name); \
if (u==univars.end()) \
{ \
if (one_warning_printed==false) \
cout << "No uniform variable " << name << " found" << endl; \
one_warning_printed = true; \
return; \
} \
GLint loc = u->second.location; \
GLenum type = u->second.type; \
\
if (type!=GLTYPE) \
{ \
cerr << "Type mismatch in setUniform for uniform variable " << name << endl; \
exit(1); \
} \
\
glUseProgram(handle); \
GLSET(loc,1,VACCESS); \
\
if (laston) \
laston->on(); \
}
#define SETUNIFORMMAT(ITYPE,GLTYPE,GLSET,VACCESS) \
void Program::setUniform ( const GLchar *name, ITYPE v ) \
{ \
map<string,unidata>::iterator u = univars.find(name); \
if (u==univars.end()) \
{ \
if (one_warning_printed==false) \
cout << "No uniform variable " << name << " found" << endl; \
one_warning_printed = true; \
return; \
} \
GLint loc = u->second.location; \
GLenum type = u->second.type; \
\
if (type!=GLTYPE) \
{ \
cerr << "Type mismatch in setUniform for uniform variable " << name << endl; \
exit(1); \
} \
\
glUseProgram(handle); \
GLSET(loc,1,false,VACCESS); \
\
if (laston) \
laston->on(); \
}
SETUNIFORM(vec2,GL_FLOAT_VEC2,glUniform2fv,&v[0])
SETUNIFORM(vec3,GL_FLOAT_VEC3,glUniform3fv,&v[0])
SETUNIFORM(vec4,GL_FLOAT_VEC4,glUniform4fv,&v[0])
SETUNIFORM(ivec2,GL_INT_VEC2,glUniform2iv,&v[0])
SETUNIFORM(ivec3,GL_INT_VEC3,glUniform3iv,&v[0])
SETUNIFORM(ivec4,GL_INT_VEC4,glUniform4iv,&v[0])
SETUNIFORM(uvec2,GL_UNSIGNED_INT_VEC2,glUniform2uiv,&v[0])
SETUNIFORM(uvec3,GL_UNSIGNED_INT_VEC3,glUniform3uiv,&v[0])
SETUNIFORM(uvec4,GL_UNSIGNED_INT_VEC4,glUniform4uiv,&v[0])
SETUNIFORM(GLfloat,GL_FLOAT,glUniform1fv,&v)
SETUNIFORM(GLint,GL_INT,glUniform1iv,&v)
SETUNIFORM(GLuint,GL_UNSIGNED_INT,glUniform1uiv,&v)
SETUNIFORMMAT(mat2,GL_FLOAT_MAT2,glUniformMatrix2fv,&v[0][0])
SETUNIFORMMAT(mat3,GL_FLOAT_MAT3,glUniformMatrix3fv,&v[0][0])
SETUNIFORMMAT(mat4,GL_FLOAT_MAT4,glUniformMatrix4fv,&v[0][0])
SETUNIFORMMAT(mat2x3,GL_FLOAT_MAT2x3,glUniformMatrix2x3fv,&v[0][0])
SETUNIFORMMAT(mat3x2,GL_FLOAT_MAT3x2,glUniformMatrix3x2fv,&v[0][0])
SETUNIFORMMAT(mat2x4,GL_FLOAT_MAT2x4,glUniformMatrix2x4fv,&v[0][0])
SETUNIFORMMAT(mat4x2,GL_FLOAT_MAT4x2,glUniformMatrix4x2fv,&v[0][0])
SETUNIFORMMAT(mat3x4,GL_FLOAT_MAT3x4,glUniformMatrix3x4fv,&v[0][0])
SETUNIFORMMAT(mat4x3,GL_FLOAT_MAT4x3,glUniformMatrix4x3fv,&v[0][0])
/* ------------------------------------------- */
void Program::printLog ( )
{
int infologLength = 0;
int maxLength;
glGetProgramiv(handle,GL_INFO_LOG_LENGTH,&maxLength);
char infoLog[maxLength];
glGetProgramInfoLog(handle, maxLength, &infologLength, infoLog);
if (infologLength > 0)
cout << infoLog << endl;
}
/* --------------------------------------------- */
Program::operator bool()
{
GLint status;
glGetProgramiv(handle,GL_LINK_STATUS,&status);
return (status==GL_TRUE);
}
/* --------------------------------------------- */
static string _type2string ( ShaderType t )
{
switch(t)
{
case Vert:
return "Vertex";
break;
case Frag:
return "Fragment";
break;
case Geom:
return "Geometry";
break;
case TessC:
return "Tesselation control";
break;
case TessE:
return "Tesselation Evaluation";
break;
default:
cout << "Unknown shader type" << endl;
exit(1);
break;
}
}
/* --------------------------------------------- */
Program * createProgram ( ShaderFile s1, ShaderFile s2, ShaderFile s3, ShaderFile s4, ShaderFile s5 )
{
ShaderFile in[5];
Shader *sh[5] = {NULL,NULL,NULL,NULL,NULL};
int ins = 0;
if (s1.name!="") in[ins++] = s1;
if (s2.name!="") in[ins++] = s2;
if (s3.name!="") in[ins++] = s3;
if (s4.name!="") in[ins++] = s4;
if (s5.name!="") in[ins++] = s5;
for ( int i=0; i<ins; i++ )
for ( int j=i+1; j<ins; j++ )
if (in[i].type==in[j].type)
{
cout << "Program cannot contain two shaders for the same pipeline stage" << endl;
exit(1);
}
bool failed = false;
for ( int i=0; i<ins; i++ )
{
string src = ReadFromFile(in[i].name.c_str());
sh[i] = new Shader(in[i].type,src);
cout << _type2string(in[i].type) << " shader log:" << endl;
sh[i]->printLog();
failed = failed || !sh[i];
}
if (failed)
{
cout << "Compilation of one of the shaders failed!" << endl;
exit(1);
}
Program *res = new Program(sh[0],sh[1],sh[2],sh[3],sh[4]);
cout << "Program info log: " << endl;
res->printLog();
if (!*res)
{
cout << "Linking failed!" << endl;
exit(1);
}
// mark shaders for deletion
for ( int i=0; i<ins; i++ )
delete sh[i];
return res;
}
};