GLuint CreateGLProgram(const char *vert_shader_src, const char *frag_shader_src) { GLuint vert_shader_id = CompileShaderFromSource(vert_shader_src, GL_VERTEX_SHADER); GLuint frag_shader_id = CompileShaderFromSource(frag_shader_src, GL_FRAGMENT_SHADER); // now we can link the program GLuint program_id = glCreateProgram(); glAttachShader(program_id, vert_shader_id); glAttachShader(program_id, frag_shader_id); glLinkProgram(program_id); GLint result; glGetProgramiv(program_id, GL_LINK_STATUS, &result); if (result != GL_TRUE) { ProgarmErrorHanlder(program_id); } glDeleteShader(vert_shader_id); glDeleteShader(frag_shader_id); return program_id; }
bool ShaderProgram::CompileShaderFromFile(const std::string &path, ShaderType type) { GLchar* source = LoadShaderSource(path); bool ret = false; if(source != NULL){ ret = CompileShaderFromSource(source, type); delete[] source; }else{ DEBUG_MESSAGE("Could not load shader source."); } return ret; }
GLuint CompileShaderFromFile(const char *fname, GLenum type) { std::string shader_text = diagrammar::Stringify(fname); const char *shader_text_cstr = shader_text.c_str(); return CompileShaderFromSource(shader_text_cstr, type); }
int ShaderProgram::CreateShaderProgramFromSource(const ShaderProgramInitDesc& source) { std::vector<ShaderInfo::ShaderCompileTypeInfo> toCompile; if (source.computeShader.size()) toCompile.push_back({ source.computeShader, ShaderType_compute }); if (source.fragmentShader.size()) toCompile.push_back({ source.fragmentShader, ShaderType_fragment }); if (source.geometryShader.size()) toCompile.push_back({ source.geometryShader, ShaderType_geometry }); if (source.tessControllShader.size()) toCompile.push_back({ source.tessControllShader, ShaderType_tessControl }); if (source.tessEvaluationShader.size()) toCompile.push_back({ source.tessEvaluationShader, ShaderType_tessEvaluation }); if (source.vertexShader.size()) toCompile.push_back({ source.vertexShader, ShaderType_vertex }); if (toCompile.size() == 0) return 0; //Create the shader program handle if (this->shaderProgramHandle == 0) { this->shaderProgramHandle = glCreateProgram(); if (this->shaderProgramHandle == 0) { this->lastError = "Failed to create gl shader program"; return 0; } } // Compile and attach the shaders to the program handle std::vector<GLuint> compiledShaders; for (size_t i = 0; i < toCompile.size(); i++) { GLuint shader = CompileShaderFromSource(toCompile[i].source, toCompile[i].shaderType); if (shader == 0) return 0; compiledShaders.push_back(shader); glAttachShader(this->shaderProgramHandle, compiledShaders[i]); } //Link the program glLinkProgram(this->shaderProgramHandle); // Check the program GLint Result = GL_FALSE; int logLen; glGetProgramiv(this->shaderProgramHandle, GL_LINK_STATUS, &Result); glGetProgramiv(this->shaderProgramHandle, GL_INFO_LOG_LENGTH, &logLen); if (Result == GL_FALSE && logLen > 0) { std::string errorMessage; errorMessage.resize(logLen + 1); glGetProgramInfoLog(this->shaderProgramHandle, logLen, NULL, &errorMessage[0]); this->lastError = errorMessage; return 0; } //Release shaders that is already linked and attached to the program handle for (size_t i = 0; i < compiledShaders.size(); i++) { glDetachShader(this->shaderProgramHandle, compiledShaders[i]); glDeleteShader(compiledShaders[i]); } return 1; }