32 FILE *file = fopen(filename,
"rb");
34 ASSERT(file != NULL,
"Cannot read shader file.");
37 fseek(file, 0, SEEK_END);
43 fseek(file, 0, SEEK_SET);
45 char *shader = (
char *)calloc(length + 1,
sizeof(
char));
47 ASSERT(shader != NULL,
"Cannot allocate memory for shader source.");
50 size_t numberOfBytesRead = fread(shader,
sizeof(
char), length, file);
52 ASSERT(numberOfBytesRead == length,
"An error ocurred while reading shader source.");
64 ASSERT(shaderObjectIdPtr != NULL,
65 "NULL pointer used to store generated shader object ID.");
67 ASSERT(shaderType == GL_FRAGMENT_SHADER || shaderType == GL_VERTEX_SHADER,
68 "Invalid shader object type.");
70 GLint compileStatus = GL_FALSE;
71 const char *
strings[1] = { NULL };
74 *shaderObjectIdPtr =
GL_CHECK(glCreateShader(shaderType));
77 GL_CHECK(glShaderSource(*shaderObjectIdPtr, 1, strings, NULL));
80 free((
void *)(strings[0]));
84 GL_CHECK(glCompileShader(*shaderObjectIdPtr));
86 GL_CHECK(glGetShaderiv(*shaderObjectIdPtr, GL_COMPILE_STATUS, &compileStatus));
89 if (compileStatus != GL_TRUE)
91 char *debugSource = NULL;
92 char *errorLog = NULL;
96 GL_CHECK(glGetShaderiv(*shaderObjectIdPtr, GL_SHADER_SOURCE_LENGTH, &length));
98 debugSource = (
char*) malloc(length);
100 if (debugSource != NULL)
102 GL_CHECK(glGetShaderSource(*shaderObjectIdPtr, length, NULL, debugSource));
104 LOGE(
"Debug source START:\n%s\nDebug source END\n\n", debugSource);
112 GL_CHECK(glGetShaderiv(*shaderObjectIdPtr, GL_INFO_LOG_LENGTH, &length));
114 errorLog = (
char*) malloc(length);
116 if (errorLog != NULL)
118 GL_CHECK(glGetShaderInfoLog(*shaderObjectIdPtr, length, NULL, errorLog));
120 LOGE(
"Log START:\n%s\nLog END\n\n", errorLog);
128 ASSERT(compileStatus == GL_TRUE,
"Shader compilation FAILED!");
static char * loadShader(const char *filename)
Load shader source from a file into memory.
GLsizei const GLchar ** strings
GLenum GLuint GLenum GLsizei length
static void processShader(GLuint *shader, const char *filename, GLint shaderType)
Create shader, load in source, compile, and dump debug as necessary.
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count