void GPU::Shader::RecoverActiveAttributes() { m_Attributes.clear(); GLint count; GLcharARB nameBuffer[512]; glGetObjectParameterivARB( m_Id, GL_OBJECT_ACTIVE_ATTRIBUTES_ARB, &count ); for( GLint i=0; i<count; ++i ) { GLsizei length; GLint size; GLenum type; glGetActiveAttribARB( m_Id, i, sizeof(nameBuffer), &length, &size, &type, nameBuffer ); GLint attribId = glGetAttribLocationARB( m_Id, nameBuffer ); if( attribId != -1 ) m_Attributes[ std::string(nameBuffer) ] = attribId; } }
static bool checkShader(GLuint object, const char * op, GLuint check) { GLint status; glGetObjectParameterivARB(object, check, &status); if(!status) { int logLength; glGetObjectParameterivARB(object, GL_OBJECT_INFO_LOG_LENGTH_ARB, &logLength); char * log = new char[logLength]; glGetInfoLogARB(object, logLength, NULL, log); LogWarning << "Failed to " << op << " vertex shader: " << log; delete[] log; return false; } return true; }
//----------------------------------------------------------------------------- bool CreateGLSLShader(char *vsFile, char *psFile, GLhandleARB &shader) { bool ret = false; // Create the shader. shader = glCreateProgramObjectARB(); // Load the vertex shader. if(LoadShader(vsFile, GL_VERTEX_SHADER_ARB, shader) == false) return false; // Load the pixel shader. if(LoadShader(psFile, GL_FRAGMENT_SHADER_ARB, shader) == false) return false; // Link together the vertex and pixel shaders. glLinkProgramARB(shader); GLint link = 0; char error[1024]; glGetObjectParameterivARB(shader, GL_OBJECT_LINK_STATUS_ARB, &link); if(!link) { // Get the error message and display it. glGetInfoLogARB(shader, sizeof(error), NULL, error); printf("Error linking shader...\n%s\n", error); return false; } return true; }
GPUShader *GPU_shader_create_lib(const char *code) { GLint status; GLcharARB log[5000]; GLsizei length = 0; GPUShader *shader; if (!GLEW_ARB_vertex_shader || !GLEW_ARB_fragment_shader) return NULL; shader = MEM_callocN(sizeof(GPUShader), "GPUShader"); shader->lib = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); if (!shader->lib) { fprintf(stderr, "GPUShader, object creation failed.\n"); GPU_shader_free(shader); return NULL; } glShaderSourceARB(shader->lib, 1, (const char**)&code, NULL); glCompileShaderARB(shader->lib); glGetObjectParameterivARB(shader->lib, GL_OBJECT_COMPILE_STATUS_ARB, &status); if (!status) { glGetInfoLogARB(shader->lib, sizeof(log), &length, log); shader_print_errors("compile", log, code); GPU_shader_free(shader); return NULL; } return shader; }
void LLPostProcess::checkShaderError(GLhandleARB shader) { GLint infologLength = 0; GLint charsWritten = 0; GLchar *infoLog; checkError(); // Check for OpenGL errors glGetObjectParameterivARB(shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &infologLength); checkError(); // Check for OpenGL errors if (infologLength > 0) { infoLog = (GLchar *)malloc(infologLength); if (infoLog == NULL) { /// Could not allocate infolog buffer return; } glGetInfoLogARB(shader, infologLength, &charsWritten, infoLog); // shaderErrorLog << (char *) infoLog << std::endl; mShaderErrorString = (char *) infoLog; free(infoLog); } checkError(); // Check for OpenGL errors }
GLuint glHelp::MakeShaderProgram(GLuint fragmentNo, GLuint vertexNo) { GLuint program_object; // a handler to the GLSL program used to update program_object = glCreateProgram(); // creating a program object if (fragmentNo != 0) { glAttachShader(program_object, fragmentNo); } if (vertexNo != 0) { glAttachShader(program_object, vertexNo); } // Link the shaders into a complete GLSL program. glLinkProgram(program_object); //printProgramInfoLog(program_object); // verifies if all this is ok so far // some extra code for checking if all this initialization is ok GLint prog_link_success; glGetObjectParameterivARB(program_object, GL_OBJECT_LINK_STATUS_ARB, &prog_link_success); if (!prog_link_success) { //fprintf(stderr, "The shaders could not be linked\n"); exit(1); } return program_object; }
void GlProgram:: link() { //attach all previously compiled shaders to the program object for(ShaderHandle it=shaders.begin(); it!=shaders.end(); ++it) glAttachObjectARB(program, *it); //link the program glLinkProgramARB(program); //check if the program linked successfully GLint linkStatus; glGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &linkStatus); if (!linkStatus) { //get some more detailed information GLcharARB linkLogBuffer[2048]; GLsizei linkLogSize; glGetInfoLogARB(program, sizeof(linkLogBuffer), &linkLogSize, linkLogBuffer); //signal an error Misc::throwStdErr("GLProgram::link: Error \"%s\" while linking shader " "program", linkLogBuffer); } }
bool GPU::Shader::PgObject::CompileSrcString( const char *sourceString, const GLenum pgType, std::string *logs ) { // Ask for a free shader object name to OpenGL. m_PgType = pgType; if( Instantiate() ) { // Create the shader object. glShaderSourceARB ( m_Id, 1, (const GLcharARB**) &sourceString, NULL ); glCompileShaderARB( m_Id ); // Check the compilation status. GLint status; glGetObjectParameterivARB( m_Id, GL_OBJECT_COMPILE_STATUS_ARB, &status ); if( status ) return true; else if( logs ) *logs = GPU::Shader::GetLogs( m_Id ); Release(); } return false; }
void printInfoLog ( GLhandleARB object ) { GLint logLength = 0; GLsizei charsWritten = 0; GLcharARB * infoLog; checkOpenGLError (); // check for OpenGL errors glGetObjectParameterivARB ( object, GL_OBJECT_INFO_LOG_LENGTH_ARB, &logLength ); if ( !checkOpenGLError() ) // check for OpenGL errors exit ( 1 ); if ( logLength > 0 ) { infoLog = (GLcharARB*) malloc ( logLength ); if ( infoLog == NULL ) { printf("ERROR: Could not allocate InfoLog buffer\n"); exit ( 1 ); } glGetInfoLogARB ( object, logLength, &charsWritten, infoLog ); printf ( "InfoLog:\n%s\n\n", infoLog ); free ( infoLog ); } if ( !checkOpenGLError () ) // check for OpenGL errors exit ( 1 ); }
/* * link - Link the shader object. * * return - bool */ bool ShaderObject::link(void) { if (linked) { std::cout << "**warning** Object is already linked, trying to link again" << std::endl; for (int i = 0; i < shaderList.size(); i++) { glDetachObjectARB(shaderObject, shaderList[i]->getProgramObject()); CHECK_GL_ERROR(); } } for (int i = 0; i < shaderList.size(); i++) { glAttachObjectARB(shaderObject, shaderList[i]->getProgramObject()); CHECK_GL_ERROR(); } int linkStatus; glLinkProgramARB(shaderObject); CHECK_GL_ERROR(); glGetObjectParameterivARB(shaderObject, GL_OBJECT_LINK_STATUS_ARB, &linkStatus); CHECK_GL_ERROR(); if (linkStatus) { linked = true; return true; } else { std::cout << "**linker error**" << std::endl; } return false; } // end link()
bool Shader::init(Text* _vertexShader, Text* _fragmentShader, const StringDict& _parameters) { if (!GL_EXTENSION_GLSL) { return false; } if (isError()) { error("opengl", "Real error before GLSL. Flushing"); } vertexShader = _vertexShader; fragmentShader = _fragmentShader; parameters = _parameters; log_msg("opengl glsl", "Compiling " + getShaderDecription()); programId = glCreateProgramObjectARB(); if (isError()) { return false; } vertexShaderId = glCreateShaderObjectARB (GL_VERTEX_SHADER_ARB); if (!loadShader(vertexShaderId, vertexShader, parameters)) { loadLog(programId); return false; } glAttachObjectARB(programId, vertexShaderId); fragmentShaderId = glCreateShaderObjectARB (GL_FRAGMENT_SHADER_ARB); if (!loadShader(fragmentShaderId, fragmentShader, parameters)) { loadLog(programId); return false; } glAttachObjectARB(programId, fragmentShaderId); GLint linked; glLinkProgramARB(programId); if (isError()) { return false; } glGetObjectParameterivARB(programId, GL_OBJECT_LINK_STATUS_ARB, &linked); loadLog(programId); if (!linked) { return false; } return true; }
//[-------------------------------------------------------] //[ Public static methods ] //[-------------------------------------------------------] uint32_t ShaderLanguageSeparate::loadShader(uint32_t shaderType, const char *sourceCode) { // Create the shader program const GLuint openGLProgram = glCreateShaderProgramv(shaderType, 1, &sourceCode); // Check the link status GLint linked = GL_FALSE; glGetObjectParameterivARB(openGLProgram, GL_LINK_STATUS, &linked); if (GL_TRUE == linked) { // All went fine, return the program return openGLProgram; } else { // Error, failed to compile the shader! #ifdef RENDERER_OUTPUT_DEBUG { // Get the length of the information GLint informationLength = 0; glGetObjectParameterivARB(openGLProgram, GL_OBJECT_INFO_LOG_LENGTH_ARB, &informationLength); if (informationLength > 1) { // Allocate memory for the information GLchar *informationLog = new GLchar[static_cast<uint32_t>(informationLength)]; // Get the information glGetInfoLogARB(openGLProgram, informationLength, nullptr, informationLog); // Output the debug string RENDERER_OUTPUT_DEBUG_STRING(informationLog) // Cleanup information memory delete [] informationLog; } } #endif // Destroy the program // -> A value of 0 for shader will be silently ignored glDeleteProgram(openGLProgram); // Error! return 0; } }
GLhandleARB CompileShader(char *ShaderAssembly,GLenum ShaderObject,GLint * bCompiled) { GLhandleARB GLSLShaderObject=glCreateShaderObjectARB(ShaderObject); glShaderSourceARB( GLSLShaderObject, 1, (const GLcharARB **)(&ShaderAssembly), NULL ); glCompileShaderARB( GLSLShaderObject); glGetObjectParameterivARB( GLSLShaderObject, GL_OBJECT_COMPILE_STATUS_ARB, bCompiled ); return GLSLShaderObject; }
void moShaderGLSL::linkProgram() { glLinkProgramARB(m_ProgramObject); GLint progLinkSuccess; glGetObjectParameterivARB(m_ProgramObject, GL_OBJECT_LINK_STATUS_ARB, &progLinkSuccess); m_VertErrorCode = progLinkSuccess; m_FragErrorCode = progLinkSuccess; if (!progLinkSuccess) if (MODebug2 != NULL) MODebug2->Error(moText("Shader program could not be linked")); }
void Shader::compile() { GLint status; glCompileShaderARB(handle); glGetObjectParameterivARB(handle, GL_OBJECT_COMPILE_STATUS_ARB, &status); if (status == GL_FALSE) { std::string errors; GLint len; // Get size of info log glGetObjectParameterivARB(handle, GL_OBJECT_INFO_LOG_LENGTH_ARB, &len); if (len > 1) { errors.resize(len); glGetInfoLogARB(handle, len, NULL, &errors[0]); } throw Exception("shader compile failed\n" + errors); } }
void LinkProgram(GLhandleARB program) { GLint logLength; GLint linked; glLinkProgramARB(program); HandleGLError("Failed glLinkProgramARB"); glGetObjectParameterivARB(program,GL_OBJECT_LINK_STATUS_ARB,&linked); glGetObjectParameterivARB(program,GL_OBJECT_INFO_LOG_LENGTH_ARB,&logLength); if (logLength) { GLint charsWritten; GLcharARB *log = (GLcharARB*)malloc(logLength+128); glGetInfoLogARB(program, logLength, &charsWritten, log); printf("Link GetInfoLogARB:\n%s\n",log); free (log); } if (!linked) { std::cout << "shader did not link" << std::endl; exit(0); } }
void STShaderProgram::LoadVertexShader(const std::string& filename) { std::ifstream in(filename.c_str()); if(!in) { fprintf(stderr, "Failed to open shader file '%s'\n", filename.c_str()); assert(false); return; } std::stringstream ss; ss << in.rdbuf(); std::string str = ss.str(); const char* ptr = str.c_str(); // Buffer for error messages static const int kBufferSize = 1024; char buffer[1024]; if(GLEW_VERSION_2_0) { GLuint shader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(shader, 1, &ptr, NULL); glCompileShader(shader); GLint result = 0; glGetShaderiv(shader, GL_COMPILE_STATUS, &result); if(result != GL_TRUE) { GLsizei length = 0; glGetShaderInfoLog(shader, kBufferSize-1, &length, buffer); fprintf(stderr, "%s: GLSL error\n%s\n", filename.c_str(), buffer); assert(false); } glAttachShader(programid, shader); glLinkProgram(programid); } #ifndef __APPLE__ else { GLuint shader = glCreateShaderObjectARB(GL_VERTEX_SHADER); glShaderSourceARB(shader, 1, &ptr, NULL); glCompileShaderARB(shader); GLint result = 0; glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &result); if(result != GL_TRUE) { GLsizei length = 0; glGetInfoLogARB(shader, kBufferSize-1, &length, buffer); fprintf(stderr, "%s: GLSL error\n%s\n", filename.c_str(), buffer); assert(false); } glAttachObjectARB(programid, shader); glLinkProgramARB(programid); } #endif }
bool GlslProgram :: loadShaders ( Data * vertexShaderData, Data * fragmentShaderData ) { ok = false; // check whether we should create program object if ( program == 0 ) program = glCreateProgramObjectARB(); // check for errors if ( !checkGlError () ) return false; // create a vertex shader object and a fragment shader object if ( vertexShaderData != NULL ) { vertexShader = glCreateShaderObjectARB ( GL_VERTEX_SHADER_ARB ); log += "Loading vertex shader\n"; if ( !loadShader ( vertexShader, vertexShaderData ) ) return false; // attach shaders to program object glAttachObjectARB ( program, vertexShader ); } if ( fragmentShaderData != NULL ) { fragmentShader = glCreateShaderObjectARB ( GL_FRAGMENT_SHADER_ARB ); log += "Loading fragment shader\n"; if ( !loadShader ( fragmentShader, fragmentShaderData ) ) return false; // attach shaders to program object glAttachObjectARB ( program, fragmentShader ); } GLint linked; log += "Linking programs\n"; // link the program object and print out the info log glLinkProgramARB ( program ); /**/ if ( !checkGlError () ) // check for errors /**/ return false; glGetObjectParameterivARB ( program, GL_OBJECT_LINK_STATUS_ARB, &linked ); loadLog ( program ); if ( !linked ) return false; return ok = true; }
unsigned int RAS_2DFilterManager::CreateShaderProgram(const char* shadersource) { GLuint program = 0; GLuint fShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER); GLint success; glShaderSourceARB(fShader, 1, (const char**)&shadersource, NULL); glCompileShaderARB(fShader); glGetObjectParameterivARB(fShader, GL_COMPILE_STATUS, &success); if(!success) { /*Shader Comile Error*/ PrintShaderErrors(fShader, "compile", shadersource); return 0; } program = glCreateProgramObjectARB(); glAttachObjectARB(program, fShader); glLinkProgramARB(program); glGetObjectParameterivARB(program, GL_LINK_STATUS, &success); if (!success) { /*Program Link Error*/ PrintShaderErrors(fShader, "link", shadersource); return 0; } glValidateProgramARB(program); glGetObjectParameterivARB(program, GL_VALIDATE_STATUS, &success); if (!success) { /*Program Validation Error*/ PrintShaderErrors(fShader, "validate", shadersource); return 0; } return program; }
void CompileProgram(GLcharARB* sourcecode, GLhandleARB *shader) { GLint logLength; GLint compiled; glShaderSourceARB(*shader,1,(const GLcharARB **)&sourcecode,0); HandleGLError("Failed glShaderSourceARB"); glCompileShaderARB(*shader); HandleGLError("Failed glCompileShaderARB"); glGetObjectParameterivARB(*shader,GL_OBJECT_COMPILE_STATUS_ARB,&compiled); glGetObjectParameterivARB(*shader,GL_OBJECT_INFO_LOG_LENGTH_ARB,&logLength); if (logLength) { GLcharARB *log = (GLcharARB *)malloc(logLength+128); glGetInfoLogARB(*shader, logLength, &logLength, log); printf("Compile log: \n"); free (log); } if (!compiled) { std::cout << "shader could not compile" << std::endl; exit(0); } }
static void printInfoLog(GLhandleARB object) { int maxLength = 0; glGetObjectParameterivARB(object, GL_OBJECT_INFO_LOG_LENGTH_ARB, &maxLength); if(maxLength < 4096) { static char infoLog[4096] ; glGetInfoLogARB(object, maxLength, &maxLength, infoLog); std::cerr << infoLog ; } else { std::cerr << "Warning: infolog was too long to fit in buffer" << std::endl ; } }
char * GetGLSLInfoLog(GLhandleARB GLSLShaderObject) { int logreadsize=0; int logbuffersize=0x800; glGetObjectParameterivARB(GLSLShaderObject, GL_OBJECT_INFO_LOG_LENGTH_ARB,&logbuffersize); if(logbuffersize<=0) return 0; char * logbuffer=new char [logbuffersize+1]; glGetInfoLogARB(GLSLShaderObject, logbuffersize,&logreadsize, logbuffer); logbuffer[logbuffersize]=0; return logbuffer; }
std::string GPU::Shader::GetLogs( GLuint pgId ) { GLint logLength; glGetObjectParameterivARB( pgId, GL_OBJECT_INFO_LOG_LENGTH_ARB, &logLength ); std::string infoLog; infoLog.resize( logLength ); glGetInfoLogARB( pgId, logLength, &logLength, &infoLog[0] ); return infoLog; }
static gboolean glsl_check_shader_error( char *msg, GLhandleARB obj ) { GLint value; glGetObjectParameterivARB(obj, GL_OBJECT_COMPILE_STATUS_ARB, &value); if( value == 0 ) { glsl_print_error(msg, obj); return FALSE; } return TRUE; }
static bool CompileShaderObject(GLhandleARB *ShaderHandle, GLenum ShaderType, GLsizei SrcStringsCount, const GLcharARB **SrcStrings) { bool Result; #ifdef USE_OPENGL_20 *ShaderHandle = glCreateShader(ShaderType); #else *ShaderHandle = glCreateShaderObjectARB(ShaderType); #endif if (*ShaderHandle != 0) { GLint CompileStatus; #ifdef USE_OPENGL_20 glShaderSource(*ShaderHandle,SrcStringsCount,SrcStrings,NULL); glCompileShader(*ShaderHandle); glGetShaderiv(*ShaderHandle,GL_COMPILE_STATUS,&CompileStatus); #else glShaderSourceARB(*ShaderHandle,SrcStringsCount,SrcStrings,NULL); glCompileShaderARB(*ShaderHandle); glGetObjectParameterivARB( *ShaderHandle, GL_OBJECT_COMPILE_STATUS_ARB, &CompileStatus); #endif Result = CompileStatus; DumpInfoLog(*ShaderHandle); if (!Result) { #ifdef USE_OPENGL_20 glDeleteShader(*ShaderHandle); #else glDeleteObjectARB(*ShaderHandle); #endif *ShaderHandle = 0; } } else { Result = false; fprintf(stderr,"error: unable to create GLSL shader object\n"); } return(Result); }
/*! \brief Fetches the build log for the passed shader * handle. */ inline std::string getShaderBuildlog(GLhandleARB shaderHandle) { std::string retVal; GLint errorLoglength; glGetObjectParameterivARB(shaderHandle, GL_OBJECT_INFO_LOG_LENGTH_ARB, &errorLoglength); retVal.resize(errorLoglength); GLsizei actualErrorLogLength; glGetInfoLogARB(shaderHandle, errorLoglength, &actualErrorLogLength, &retVal[0]); return retVal; }
int Shader::installShaders() { GLhandleARB vs, fs; GLint vertCompiled, fragCompiled; // Criar objectos vs = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); fs = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); // Carregar strings glShaderSourceARB(vs, 1, (const GLcharARB**)&m_VertShdStr, NULL); glShaderSourceARB(fs, 1, (const GLcharARB**)&m_FragShdStr, NULL); glCompileShaderARB(vs); glGetObjectParameterivARB(vs, GL_OBJECT_COMPILE_STATUS_ARB, &vertCompiled); if (!vertCompiled) { // console->printfAndRender(1.0f, 0.0f, 0.0f, "Error compiling vertex shader '%s':", strrchr(m_FileName, '\\') + 1); printInfoLog(vs); return false; } glCompileShaderARB(fs); glGetObjectParameterivARB(fs, GL_OBJECT_COMPILE_STATUS_ARB, &fragCompiled); if (!fragCompiled) { // console->printfAndRender(1.0f, 0.0f, 0.0f, "Error compiling fragment shader '%s':", strrchr(m_FileName, '\\') + 1); printInfoLog(fs); return false; } // Criar um objecto e ligar os dois shaders já compilados m_Handle = glCreateProgramObjectARB(); glAttachObjectARB(m_Handle, vs); glAttachObjectARB(m_Handle, fs); Link(); return true; }
bool loadShader ( GLhandleARB shader, const char * fileName ) { printf ( "Loading %s\n", fileName ); FILE * file = fopen ( fileName, "rb" ); if ( file == NULL ) { printf ( "Error opening %s\n", fileName ); exit ( 1 ); } fseek ( file, 0, SEEK_END ); GLint size = ftell ( file ); if ( size < 1 ) { fclose ( file ); printf ( "Error loading file %s\n", fileName ); exit ( 1 ); } char * buf = (char *) malloc ( size ); fseek ( file, 0, SEEK_SET ); if ( fread ( buf, 1, size, file ) != size ) { fclose ( file ); printf ( "Error loading file %s\n", fileName ); exit ( 1 ); } fclose ( file ); GLint compileStatus; glShaderSourceARB ( shader, 1, (const char **) &buf, &size ); free ( buf ); // compile the particle vertex shader, and print out glCompileShaderARB ( shader ); if ( !checkOpenGLError() ) // check for OpenGL errors return false; glGetObjectParameterivARB ( shader, GL_OBJECT_COMPILE_STATUS_ARB, &compileStatus ); printInfoLog ( shader ); return compileStatus != 0; }
GLenum GLShader::LinkProgram(GLhandleARB program) { GLint logLength; GLint linked; glLinkProgramARB(program); FailGLError("Failed glLinkProgramARB"); glGetObjectParameterivARB(program,GL_OBJECT_LINK_STATUS_ARB,&linked); glGetObjectParameterivARB(program,GL_OBJECT_INFO_LOG_LENGTH_ARB,&logLength); if (logLength) { GLint charsWritten; GLcharARB *log = (GLcharARB *)malloc(logLength+128); glGetInfoLogARB(program, logLength, &charsWritten, log); Logger::log(LOG_ERROR,"Link GetInfoLogARB:\n%s\n",log); free (log); } if (!linked) FailWithMessage("shader did not link"); return GL_NO_ERROR; }
static inline std::string glsl_log(GLuint handle) { if (handle == 0) return ""; GLint log_len; glGetObjectParameterivARB(handle, GL_OBJECT_INFO_LOG_LENGTH_ARB, &log_len); if (log_len == 0) return ""; std::string log(log_len, '\0'); GLsizei written; glGetInfoLogARB(handle, log_len, &written, (GLcharARB *)log.c_str()); log.resize(written); return log; }