Search code examples
openglglslspir-v

How to use OpenGL ARB_gl_spirv extension?


I would like to compile my GLSL shaders to SPIR-V binaries, and use that in my OpenGL project.

I've found glslang, using I can compile the glsl shaders to spir-v. But I haven't found any tutorials about how to use it in my C++ project. How to load these binaries and create shader programs from them?


Solution

  • Load the SPIR-V binary just like you're loading any other binary file in C++. Then, when you're compiling shaders, you must call glShaderBinary and glSpecializeShader:

    GLuint vertexShader = glCreateShader( GL_VERTEX_SHADER );
    glShaderBinary( 1, &vertexShader, GL_SHADER_BINARY_FORMAT_SPIR_V_ARB, vertexData, sizeof( vertexData ) ); // vertexData is the SPIR-V file contents
    glSpecializeShader( vertexShader, "main", 0, nullptr, nullptr );
    glAttachShader( program, vertexShader );