Search code examples
iosobjective-copengl-esglslshader

OpenGL ES2: syntax error when using "layout" keyword in vertex shader(iOS)


I'm new to iPhone OpenGL ES development. When I try to specify the location of a vertex attribute using the 'layout' keyword, the compiler complains there is a syntax error. Am I using it wrong? or the compiler doesn't support the keyword? I know there are other ways to specify the location, but I wonder why this one doesn't work for me.
BTW, I'm using OpenGLES2.0 API.

the vertex shader is:

layout(location = 0) in vec3 position;

void main() {
    gl_Position = vec4(position, 1.0);
}

vertex shader compile log:

2019-12-25 10:47:48.518110+0800 OpenGLESTest[14556:1063881] compile vertex shader error!
2019-12-25 10:47:48.518284+0800 OpenGLESTest[14556:1063881] compile stage info log 
ERROR: 0:1: 'layout' : syntax error: syntax error

Solution

  • In OpenGL ES 2.0 and OpenGL ES Shading Language 1.00 neither Layout Qualifiers not the keyword in is supported.
    If you want to use layout qualifiers, you've to switch to OpenGL ES 3.00 and OpenGL ES Shading Language 3.00. See Khronos OpenGL ES Registry.

    Correct GLSL ES 1.00 vertex shader (corresponding to OpenGL ES 2.00):

    #version 100
    
    attribute vec3 position;
    
    void main() {
        gl_Position = vec4(position, 1.0);
    }