How to do it...

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 lengthGLint length = 0;glGetProgramiv(program, GL_PROGRAM_BINARY_LENGTH, &length);// Retrieve the binary codestd::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" ...

Get OpenGL 4 Shading Language Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.