OpenGL 4.1 introduced the glGetProgramBinary
and glProgramBinary
functions, which allow us to save and load compiled shader program binaries. Note that this functionality is still quite dependent on the OpenGL driver, and is not widely supported. For example, the Intel drivers on macOS do not support any binary formats.
In this recipe, we'll outline the steps involved in saving and loading a compiled shader program.
We'll begin assuming that a shader program has been successfully compiled, and its ID is in the program
variable.
To save the shader binary, first verify that the driver supports at least one shader binary format:
GLint formats = 0; glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &formats); if( formats < 1 ) { std::cerr << "Driver does not support any binary formats." << std::endl; exit(EXIT_FAILURE); }
Then, assuming at least one binary format is available, use glGetProgramBinary
to retrieve the compiled shader code and write it to a file:
// Get the binary length GLint length = 0; glGetProgramiv(program, GL_PROGRAM_BINARY_LENGTH, &length); // Retrieve the binary code std::vector<GLubyte> buffer(length); GLenum format = 0; glGetProgramBinary(program, length, NULL, &format, buffer.data()); // Write the binary to a file. std::string fName("shader.bin"); std::cout << "Writing to " << fName << ", binary format = " <<format << std::endl; std::ofstream out(fName.c_str(), std::ios::binary); out.write( reinterpret_cast<char *>(buffer.data()), length ); out.close();
To load and use a shader binary, retrieve the compiled program from storage, and use glProgramBinary
to load it into the OpenGL context:
GLuint program = glCreateProgram(); // Load binary from file std::ifstream inputStream("shader.bin", std::ios::binary); std::istreambuf_iterator<char> startIt(inputStream), endIt; std::vector<char> buffer(startIt, endIt); // Load file inputStream.close(); // Install shader binary glProgramBinary(program, format, buffer.data(), buffer.size() ); // Check for success/failure GLint status; glGetprogramiv(program, GL_LINK_STATUS, &status); if( GL_FALSE == status ) { // Handle failure ... }
Drivers can support zero or more binary formats. The call to glGetIntegerv
with the GL_NUM_PROGRAM_BINARY_FORMATS
constant queries the driver to see how many are available. If this number is zero, the OpenGL driver does not support reading or writing shader binaries. If the value is one or more, we're good to go.
If at least one binary format is available, we can use glGetProgramBinary
to retrieve the compiled shader code shown earlier. The function will write the binary format used to the location pointed to by the fourth parameter. In the preceding example, the data is stored in the vector named buffer
.
To load the shader binary, we can use glProgramBinary
. This function will load a previously saved shader binary. It requires the binary format to be passed as the second parameter. We can then check GL_LINK_STATUS
to verify that it was loaded without error.