OpenGL 4.1 Allows the Use Of Binary Shaders

OpenGL



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:

7 thoughts on “OpenGL 4.1 Allows the Use Of Binary Shaders”

  1. Leith

    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)

  2. Pingback: NVIDIA R259.09: First OpenGL 4.1 Drivers Expose and 9 New Extensions - 3D Tech News, Pixel Hacking, Data Visualization and 3D Programming - Geeks3D.com

  3. Pingback: [Test] Want to See a GLSL Binary Shader (GL_ARB_get_program_binary)? - 3D Tech News, Pixel Hacking, Data Visualization and 3D Programming - Geeks3D.com

  4. Leith Bade

    PROGRAM_BINARY_RETRIEVABLE_HINT should be GL_PROGRAM_BINARY_RETRIEVABLE_HINT

Comments are closed.