2. GLSL Validation
Posted on : 25-03-2010 | By : Swiftless | In : GLSL
Tags: GLSL, OpenGL, programming, tutorial, validate
0
Jump To:
shader.h Source
shader.cpp Source
main.cpp Source
shader.vert Source
shader.frag Source
Download
When developing with C++, it is often handy when the compiler tells you where a problem is. With GLSL validation, we can find out where exactly we have an error within our shader program.
This tutorial is going to show you how to validate a GLSL program. If your shaders fails validation, the output will be written to the console, and will hopefully help you find the problem.
Let get to it..
Shader.H File: This tutorial is going to start off nice and simple, no changes will be made to this file
| ||||
Shader.CPP File: Shader.cpp is where we are going to make *all* of our changes. To get validation working, we are going to add two new methods. One for validating the seperate vertex and fragment shaders, and one for validating the final shader program. We are going to call these methods validateShader and validateProgram. validateShader will take two parameters, the first being the shader we want the check, and the second being the filename of the file associated with the shader. static void validateShader(GLuint shader, const char* file = 0) We are going to need three variables for this. The first is just the size of the output buffer that we want. This can be any value, but 512 sounds nice. const unsigned int BUFFER_SIZE = 512; The second variable will be an array of char’s and will be used to contain the output from our validation. We then make a call to memset to set the char array to all 0′s. char buffer[BUFFER_SIZE]; The final variable will be called length, and guess what. It will contain the length of the buffer we get back from OpenGL. GLsizei length = 0; All we have to do now, is read back the shader log from opengl and store it in our buffer. glGetShaderInfoLog(shader, BUFFER_SIZE, &length, buffer); Finally, we just output the information if the length of the returned string is greater than 0. Meaning we have something to output. if (length > 0) { The next method, validateProgram will take one parameter, and this will be an integer associated with the shader we are validating. static void validateProgram(GLuint program) The first few lines of the method are practically identical to that of the previous. We just want to change the output from the shader log: const unsigned int BUFFER_SIZE = 512; Here, we now want to state that we have a problem linking the shaders, and output the error. cerr << “Program “ << program << “ link error: “ << buffer << endl; Now for the new parts, we want to make a call to validate the program, and then using an integer, read back the status of the shader. glValidateProgram(program); Once we have read back the status, we check to make sure the status is GL_TRUE, if it is GL_FALSE, then we have an error, and we output which GLSL shader program had the error. if (status == GL_FALSE)
Then we just have to call the methods we have just created. These are called from within the init() method. I am going to call the validation methods after the respective compile calls for the shaders like so: glCompileShader(shader_vp); And then at the end of the init method, make a call to validateProgram: validateProgram(shader_id); That is the end of this tutorial, the rest of the page simply contains the source code for this tutorial. If you have any questions, just email me at swiftless@gmail.com
|





