Compiling Shaders
Writing shaders for use with OpenGL programs is similar to using a compiler-based language like C. You have a compiler analyze your program, check it for errors, and then translate it into object code. Next, you combine a collection of object files together in a linking phase to generate an executable program. Using GLSL shaders in your program is a similar process, except that the compiler and linker are part of the OpenGL API.
Figure 2.1 illustrates the steps to create GLSL shader objects and link them to create an executable shader program.
Figure 2.1 Shader-compilation command sequence
For each shader program you want to use in your application, you’ll need to do the following sequence of steps:
For each shader object:
Create a shader object.
Compile your shader source into the object.
Verify that your shader compiled successfully.
Then, to link multiple shader objects into a shader program, you’ll
Create a shader program.
Attach the appropriate shader objects to the shader program.
Link the shader program.
Verify that the shader link phase completed successfully.
Use the shader for processing.
Why create multiple shader objects? Just as you might reuse a function in different programs, the same idea applies to GLSL programs. Common routines that you create might be usable in multiple shaders. Instead of having to compile several large shaders with lots of common code, you’ll merely link the appropriate shader objects into a shader program.
To create a shader object, call glCreateShader().
Once you have created a shader object, you need to associate the source code of the shader with that object created by glCreateShader(). This is done by calling glShaderSource().
To compile a shader object’s source, use glCompileShader().
Similar to when you compile a C program, you need to determine if the compilation finished successfully. A call to glGetShaderiv(), with an argument of GL_COMPILE_STATUS, will return the status of the compilation phase. If GL_TRUE is returned, the compilation succeeded, and the object can be linked into a shader program. If the compilation failed, you can determine what the error was by retrieving the compilation log. glGetShaderInfoLog() will return an implementation-specific set of messages describing the compilation errors. The current size of the error log can be queried by calling glGetShaderiv() with an argument of GL_INFO_LOG_LENGTH.
Once you have created and compiled all of the necessary shader objects, you will need to link them to create an executable shader program. This process is similar in nature to creating shader objects. First, you’ll need to create a shader program to which you can attach the shader objects. When you use glCreateProgram(), a shader program will be returned for further processing.
Once you have your shader program, you’ll need to populate it with the necessary shader objects to create the executable program. This is accomplished by attaching a shader object to the program by calling glAttachShader().
For parity, if you need to remove a shader object from a program to modify the shader’s operation, detach the shader object by calling glDetachShader() with the appropriate shader object identifier.
After all the necessary shader objects have been attached to the shader program, you will need to link the objects for an executable program. This is accomplished by calling glLinkProgram().
As with shader objects, there’s a chance that the linking phase may fail due to errors in the attached shader objects. You can query the result of the link operation’s success by calling glGetProgramiv() with an argument of GL_LINK_STATUS. If GL_TRUE was returned, the link was successful, and you’re able to specify the shader program for use in processing vertices or fragments. If the link failed, represented by GL_FALSE being returned, you can determine the cause of the failure by retrieving the program link information log by calling glGetProgramInfoLog().
After a successful program link, you can execute the shaders in the program by calling glUseProgram() with the program’s object handle.
When you’re done using a shader object, you can delete it using glDeleteShader(), even if it’s attached to an active program. Just as in linking a C program, once you have an executable, you don’t need the object files until you compile again.
Similarly, if you’re done using a shader program, you can delete it by calling glDeleteProgram().
Finally, for completeness, you can also determine if a name is already been reserved as a shader object by calling glIsShader() or a shader program by calling glIsProgram():
To simplify using shaders in your applications, we created LoadShaders() to help in loading and creating shader programs. We used it in our first program in Chapter 1 to load a simple set of shaders.