How do I declare the OpenGL version in shaders on Android?

5.4k views Asked by At

I'm experimenting with OpenGL ES 3.0 and found the following statement in the Quick Reference sheet:

“#version 300 es” must appear in the first line of a shader program written in GLSL ES version 3.00. If omitted, the shader will be treated as targeting version 1.00.

So I tried to add this at the beginning of my shaders, but that only resulted in the error

Link failed because of invalid vertex shader.

as reported by .glGetProgramInfoLog. If I remove the first line with the "#version 300 gl" statement, the shader compiles and works.

This is the code of my vertex shader

private final String vertexShaderCode =
        "#version 300 es \n" +
        "uniform mat4 uMVPMatrix; \n" +
        "attribute vec2 a_TexCoordinate; \n" +
        "attribute vec4 vPosition; \n" +
        "varying vec2 v_TexCoordinate; \n" +
        "void main() { \n" +
        "  v_TexCoordinate = a_TexCoordinate; \n" +
        "  gl_Position = uMVPMatrix * vPosition; \n" +
        "} \n";

I also added the version statement to the vertex and the fragment shader, and still get the same error.

I call setEGLContextClientVersion(3) in my GLSurfaceView, and I added <uses-feature android:glEsVersion="0x00030000" android:required="true" /> to my manifest to indicate that the app requires OpenGL ES 3.0.

Am I reading the OpenGL ES documentation wrong and I don't need to add this version statement? If I need to add it, what am I doing wrong that it always results in an error?

1

There are 1 answers

0
umlum On BEST ANSWER

Reading the GLSL ES3.0 spec it lists "attribute" and "varying" as reserved keywords that will result in an error.

In GLES3, you must qualify input variables with "in" and output variables with "out".

So in the vertex shader,

attribute -> in
varying   -> out

And in the fragment shader

varying -> in

Section 4.3 in the spec (storage qualifiers) has all the details.