OpenGL 4.1 Allows the Use Of Binary Shaders

Hey cool news, among the new specifications of OpenGL 4.1, there is this one:
- The ability to query and load a binary for shader program objects to save re-compilation time
This is possible via the new GL_ARB_get_program_binary extension that makes it possible to handle the binary representation of an ascii GLSL shader. Here is roughly how this new extension works:
1/ Save a GLSL shader into a file
GLuint progId = glCreateProgram(); ... ... glProgramParameteri(progId, PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE); glLinkProgram(progId); ... ... #define GL_NUM_PROGRAM_BINARY_FORMATS 0x87FE #define GL_PROGRAM_BINARY_FORMATS 0x87FF GLint formats = 0; glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &formats); GLint *binaryFormats = new GLint[formats]; glGetIntegerv(GL_PROGRAM_BINARY_FORMATS, binaryFormats); #define GL_PROGRAM_BINARY_LENGTH 0x8741 GLint len=0; glGetProgramiv(progId, GL_PROGRAM_BINARY_LENGTH, &len); u8* binary = new u8[len]; GLenum *binaryFormats = 0; glGetProgramBinary(progId, len, NULL, (GLenum*)binaryFormats, binary); FILE* fp = fopen(shader.bin, "wb"); fwrite(binary, len, 1, fp); fclose(fp); delete [] binary;
2/ Load a binary GLSL shader from a file
FILE* fp = fopen(shader.bin, "rb");
fseek(fp, 0, SEEK_END);
GLint len = (GLint)ftell(fp);
u8* binary = new u8[len];
fseek(fp, 0, SEEK_SET);
fread(binary, len, 1, fp);
fclose(fp);
GLint formats = 0;
glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &formats)
GLenum *binaryFormats = new [formats];
glGetIntegerv(GL_PROGRAM_BINARY_FORMATS, binaryFormats);
GLuint progId = glCreateProgram();
glProgramBinary(progId, binaryFormats, binary, len);
delete [] binary;
GLint success;
glGetProgramiv(progId, GL_LINK_STATUS, &success);
if (!success)
{
// Loading failed...
}
Actually as soon as NVIDIA will release a GL 4.1 driver, I will test this extension because I’m sure we can use it with an OpenGL 2 context, as long as the driver exposes GL_ARB_get_program_binary…
More links about OpenGL 4.1:
- Khronos Group releases OpenGL 4.1, claims to leapfrog Direct3D 11
- Khronos Drives Evolution of Cross-Platform 3D Graphics with Release of OpenGL 4.1 Specification
- OpenGL 4.1 API Quick Reference Card
Tweet
[ Subscribe to Geeks3D latest news by email ]














Unfortunately it looks like they decided to not support OGL 2.0 (from http://developer.nvidia.com/object/opengl_driver.html):
For OpenGL 2 capable hardware, these new extensions are provided:
* ARB_debug_output
* ARB_ES2_compatibility (also in core OpenGL 4.1)
* ARB_separate_shader_objects (also in core OpenGL 4.1)
For OpenGL 3 capable hardware, these new extensions are provided:
* ARB_get_program_binary (also in core OpenGL 4.1)
Will this binary format be compatable between vendors?
[...] updating GPU Caps Viewer with OpenGL 4.1 support. I quickly tested if GL_ARB_get_program_binary is exposed with an OpenGL 2.1 context and the answer is yes. More on this very soon. The new [...]
[...] I said in this news, I’ll test as soon as possible the GL_ARB_get_program_binary extension with an OpenGL 2 [...]
Wow not bad only fours years after DirectX
PROGRAM_BINARY_RETRIEVABLE_HINT should be GL_PROGRAM_BINARY_RETRIEVABLE_HINT
Variable `binaryFormats’ is redefined on line 22…