Example/GLSL Shader Compile Error Testing: Difference between revisions

From OpenGL Wiki
Jump to navigation Jump to search
Adrian (talk | contribs)
m simplifying
Using bad coding style (ie: not initializing variables) is NOT simplifying.
 
(One intermediate revision by one other user not shown)
Line 9: Line 9:
glCompileShader(shader);
glCompileShader(shader);


GLint isCompiled;
GLint isCompiled = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
if(isCompiled == GL_FALSE)
if(isCompiled == GL_FALSE)
{
{
GLint maxLength;
GLint maxLength = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);


// The maxLength includes the NULL character
// The maxLength includes the NULL character
GLchar errorLog[maxLength];
std::vector<GLchar> errorLog(maxLength);
glGetShaderInfoLog(shader, maxLength, &maxLength, errorLog);
glGetShaderInfoLog(shader, maxLength, &maxLength, &errorLog[0]);
 
// 'errorLog' now contains information that can e.g. be printed to stderr


// Provide the infolog in whatever manor you deem best.
// Exit with failure.
// Exit with failure.
glDeleteShader(shader); // Don't leak the shader.
glDeleteShader(shader); // Don't leak the shader.

Latest revision as of 14:21, 6 December 2014

Shader compilation error checking.

GLuint shader = glCreateShader(...);

// Get strings for glShaderSource.
glShaderSource(shader, ...);

glCompileShader(shader);

GLint isCompiled = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
if(isCompiled == GL_FALSE)
{
	GLint maxLength = 0;
	glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);

	// The maxLength includes the NULL character
	std::vector<GLchar> errorLog(maxLength);
	glGetShaderInfoLog(shader, maxLength, &maxLength, &errorLog[0]);

	// Provide the infolog in whatever manor you deem best.
	// Exit with failure.
	glDeleteShader(shader); // Don't leak the shader.
	return;
}

// Shader compilation is successful.