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:




Geeks3D.com

↑ Grab this Headline Animator