-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshader_setup.cpp
More file actions
403 lines (243 loc) · 9.01 KB
/
shader_setup.cpp
File metadata and controls
403 lines (243 loc) · 9.01 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include "shader_setup.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <iostream>
#include <fstream>
using namespace std;
// private function declarations
static GLSL_ERROR createShaderFromFile(GLenum shaderType, const string& shaderFilePath, GLuint *shaderObject, const string **shaderSource);
static const string *shaderSourceStringFromFile(const string& filePath);
static void printSourceListing(const string& sourceString, bool showLineNumbers=true);
static void reportProgramInfoLog(GLuint program);
static void reportShaderInfoLog(GLuint shader);
// main shader loader function
GLuint setupShaders(const string& vsPath, const string& fsPath, GLSL_ERROR *error_result) {
GLuint vertexShader=0, fragmentShader=0, glslProgram = 0;
const string *vertexShaderSource = NULL, *fragmentShaderSource = NULL;
// create vertex shader object
GLSL_ERROR err = createShaderFromFile(GL_VERTEX_SHADER, vsPath, &vertexShader, &vertexShaderSource);
if (err != GLSL_OK) {
switch(err) {
case GLSL_SHADER_SOURCE_NOT_FOUND:
printf("Vertex shader source not found. Ensure the GUShaderSource object for the vertex shader has been created successfully.\n");
if (error_result)
*error_result = GLSL_VERTEX_SHADER_SOURCE_NOT_FOUND;
return 0;
case GLSL_SHADER_OBJECT_CREATION_ERROR:
printf("OpenGL could not create the vertex shader program object. Try using fewer resources before creating the program object.\n");
if (error_result)
*error_result = GLSL_VERTEX_SHADER_OBJECT_CREATION_ERROR;
return 0;
case GLSL_SHADER_COMPILE_ERROR:
printf("The vertex shader could not be compiled successfully...\n");
printf("Vertex shader source code...\n\n");
printSourceListing(*vertexShaderSource);
// report compilation error log
printf("\n<vertex shader compiler errors--------------------->\n\n");
reportShaderInfoLog(vertexShader);
printf("<-----------------end vertex shader compiler errors>\n\n\n");
// dispose of existing shader objects
glDeleteShader(vertexShader);
if (vertexShaderSource)
delete vertexShaderSource;
if (error_result)
*error_result = GLSL_VERTEX_SHADER_COMPILE_ERROR;
return 0;
default:
printf("The vertex shader object could not be created successfully.\n");
// dispose of existing shader objects
glDeleteShader(vertexShader);
if (vertexShaderSource)
delete vertexShaderSource;
if (error_result)
*error_result = err;
return 0;
}
}
// create fragment shader object
err = createShaderFromFile(GL_FRAGMENT_SHADER, fsPath, &fragmentShader, &fragmentShaderSource);
if (err != GLSL_OK) {
switch(err) {
case GLSL_SHADER_SOURCE_NOT_FOUND:
printf("Fragment shader source not found. Ensure the GUShaderSource object for the fragment shader has been created successfully.");
// dispose of existing shader objects
glDeleteShader(vertexShader);
if (vertexShaderSource)
delete vertexShaderSource;
if (error_result)
*error_result = GLSL_FRAGMENT_SHADER_SOURCE_NOT_FOUND;
return 0;
case GLSL_SHADER_OBJECT_CREATION_ERROR:
printf("OpenGL could not create the fragment shader program object. Try using fewer resources before creating the program object.\n");
// dispose of existing shader objects
glDeleteShader(vertexShader);
if (vertexShaderSource)
delete vertexShaderSource;
if (error_result)
*error_result = GLSL_FRAGMENT_SHADER_OBJECT_CREATION_ERROR;
return 0;
case GLSL_SHADER_COMPILE_ERROR:
printf("The fragment shader could not be compiled successfully...\n");
printf("Fragment shader source code...\n\n");
printSourceListing(*fragmentShaderSource);
// report compilation error log
printf("\n<fragment shader compiler errors--------------------->\n\n");
reportShaderInfoLog(fragmentShader);
printf("<-----------------end fragment shader compiler errors>\n\n");
// dispose of existing shader objects
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
if (vertexShaderSource)
delete vertexShaderSource;
if (fragmentShaderSource)
delete fragmentShaderSource;
if (error_result)
*error_result = GLSL_FRAGMENT_SHADER_COMPILE_ERROR;
return 0;
default:
printf("The fragment shader object could not be created successfully.\n");
// dispose of existing shader objects
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
if (vertexShaderSource)
delete vertexShaderSource;
if (fragmentShaderSource)
delete fragmentShaderSource;
if (error_result)
*error_result = err;
return 0;
}
}
// source code objects not longer needed
if (vertexShaderSource)
delete vertexShaderSource;
if (fragmentShaderSource)
delete fragmentShaderSource;
//
// Once vertex and fragment shader objects have been validated, setup the main
// shader program object
//
glslProgram = glCreateProgram();
if (!glslProgram) {
printf("The shader program object could not be created.\n");
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
if (error_result)
*error_result = GLSL_PROGRAM_OBJECT_CREATION_ERROR;
return 0;
}
// Attach shader objects
glAttachShader(glslProgram, vertexShader);
glAttachShader(glslProgram, fragmentShader);
// link the shader program
glLinkProgram(glslProgram);
// validate link status
GLint linkStatus;
glGetProgramiv(glslProgram, GL_LINK_STATUS, &linkStatus);
if (linkStatus==0) {
// failed to link - report linker error log and dispose of local resources
printf("The shader program object could not be linked successfully...\n");
// report linker error log
printf("\n<GLSL shader program object linker errors--------------------->\n\n");
reportProgramInfoLog(glslProgram);
printf("<-----------------end shader program object linker errors>\n\n");
// delete program and detach shaders
glDeleteProgram(glslProgram);
// delete shaders
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
if (error_result)
*error_result = GLSL_PROGRAM_OBJECT_LINK_ERROR;
return 0;
}
// cleanup - delete individual shader objects since they're now included as part of the overall shader program object
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
if (error_result)
*error_result = GLSL_OK;
return glslProgram;
}
//
// private function implementation
//
GLSL_ERROR createShaderFromFile(GLenum shaderType, const string& shaderFilePath, GLuint *shaderObject, const string **shaderSource) {
const string *sourceString = shaderSourceStringFromFile(shaderFilePath);
if (!sourceString)
return GLSL_SHADER_SOURCE_NOT_FOUND;
const char *src = sourceString->c_str();
if (!src) {
delete sourceString;
return GLSL_SHADER_OBJECT_CREATION_ERROR;
}
GLuint shader = glCreateShader(shaderType);
if (shader==0) {
delete sourceString;
return GLSL_SHADER_OBJECT_CREATION_ERROR;
}
glShaderSource(shader, 1, static_cast<const GLchar**>(&src), 0);
glCompileShader(shader);
*shaderObject = shader;
*shaderSource = sourceString;
GLint compileStatus;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus);
return (compileStatus==0) ? GLSL_SHADER_COMPILE_ERROR : GLSL_OK;
}
const string *shaderSourceStringFromFile(const string& filePath) {
string *sourceString = NULL;
const char *file_str = filePath.c_str();
struct stat fileStatus;
int file_error = stat(file_str, &fileStatus);
if (file_error==0) {
_off_t fileSize = fileStatus.st_size;
char *src = (char *)calloc(fileSize+1, 1); // add null-terminator character at end of string
if (src) {
ifstream shaderFile(file_str);
if (shaderFile.is_open()) {
shaderFile.read(src, fileSize);
sourceString = new string(src);
shaderFile.close();
}
// dispose of local resources
free(src);
}
}
// return pointer to new source string
return sourceString;
}
void printSourceListing(const string& sourceString, bool showLineNumbers) {
const char *srcPtr = sourceString.c_str();
const char *srcEnd = srcPtr + sourceString.length();
size_t lineIndex = 0;
while (srcPtr < srcEnd) {
if (showLineNumbers) {
cout.fill(' ');
cout.width(4);
cout << dec << ++lineIndex << " > ";
}
size_t substrLength = strcspn(srcPtr, "\n");
cout << string(srcPtr, 0, substrLength) << endl;
srcPtr += substrLength + 1;
}
}
void reportProgramInfoLog(GLuint program)
{
GLsizei noofBytes = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &noofBytes);
GLchar *str = (GLchar*)calloc(noofBytes+1, 1);
if (str) {
glGetProgramInfoLog(program, noofBytes, 0, str);
printf("%s\n", str);
free(str);
}
}
void reportShaderInfoLog(GLuint shader)
{
GLsizei noofBytes = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &noofBytes);
GLchar *str = (GLchar*)calloc(noofBytes+1, 1);
if (str) {
glGetShaderInfoLog(shader, noofBytes, 0, str);
printf("%s\n", str);
free(str);
}
}