GLuint CreateShader(GLenum type, const char* filename) { GLuint shader = glCreateShader(type); char* src; unsigned long srcLen; LoadShaderSource(filename, &src, &srcLen); GLint logLength; GLint compileStatus; glShaderSource(shader, 1, &src, NULL); glCompileShader(shader); glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength); if (logLength > 0) { GLchar* log = (GLchar*)malloc(sizeof(GLchar) * logLength); glGetShaderInfoLog(shader, logLength, &logLength, log); std::cout << "Shader Info Log from " << filename << ": "<< std::endl; std::cout << log << std::endl; free(log); } glGetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus); assert(compileStatus); FreeShaderSource(&src); return shader; }
GL_API void GL_APIENTRY glShaderSource(GLuint shader, GLsizei count, const char **string, const GLint *length) { State * state = GLES_GET_STATE(); GLuint index, totalLength = 0; char * text; Shader * shaderObject = GlesGetShaderObject(state, shader); if (!shaderObject) { return; } FreeShaderSource(shaderObject); for (index = 0; index < count; ++index) { const char * source = string[index]; GLuint lineLength = 0; if (source) { if (length && length[index] >= 0) { lineLength = length[index]; } else { lineLength = strlen(source); } } totalLength += lineLength; } text = shaderObject->text = (char *) GlesMalloc(totalLength + 1); if (!text) { GlesRecordOutOfMemory(state); return; } shaderObject->length = totalLength; for (index = 0; index < count; ++index) { const char * source = string[index]; if (source) { GLuint lineLength; if (length && length[index] >= 0) { lineLength = length[index]; } else { lineLength = strlen(source); } GlesMemcpy(text, source, lineLength); text += lineLength; } } /* add string terminator */ *text = '\0'; }
void GlesDeleteShader(State * state, Shader * shader) { GLES_ASSERT(shader - state->shaders >= 0 && shader - state->shaders < GLES_MAX_SHADERS); FreeShaderSource(shader); FreeShaderIntermediate(shader); GlesLogDeInit(&shader->log); GlesUnbindObject(state->shaderFreeList, GLES_MAX_SHADERS, shader - state->shaders); }
// // Read a file's data into a string, and compile it using ShCompile // bool CompileFile(char* fileName, ShHandle compiler, int compileOptions) { ShaderSource source; if (!ReadShaderSource(fileName, source)) return false; int ret = ShCompile(compiler, &source[0], source.size(), compileOptions); FreeShaderSource(source); return ret ? true : false; }