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 };
75 *shaderObjectIdPtr =
GL_CHECK(glCreateShader(shaderType));
82 GL_CHECK(glShaderSource(*shaderObjectIdPtr, 1, strings, NULL));
86 free((
void *)(strings[0]));
92 GL_CHECK(glCompileShader(*shaderObjectIdPtr));
96 GL_CHECK(glGetShaderiv(*shaderObjectIdPtr, GL_COMPILE_STATUS, &compileStatus));
100 if (compileStatus != GL_TRUE)
102 char *debugSource = NULL;
103 char *errorLog = NULL;
107 GL_CHECK(glGetShaderiv(*shaderObjectIdPtr, GL_SHADER_SOURCE_LENGTH, &length));
109 debugSource = (
char*) malloc(length);
111 if (debugSource != NULL)
113 GL_CHECK(glGetShaderSource(*shaderObjectIdPtr, length, NULL, debugSource));
115 LOGE(
"Debug source START:\n%s\nDebug source END\n\n", debugSource);
123 GL_CHECK(glGetShaderiv(*shaderObjectIdPtr, GL_INFO_LOG_LENGTH, &length));
125 errorLog = (
char*) malloc(length);
127 if (errorLog != NULL)
129 GL_CHECK(glGetShaderInfoLog(*shaderObjectIdPtr, length, NULL, errorLog));
131 LOGE(
"Log START:\n%s\nLog END\n\n", errorLog);
139 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