androidopengl-esshaderglsles

OpenGL/Android GLSL ES 3.00 only


My goal is to use OpenGL 3.1 in my Android app to display a texture. Currently the problem is, that the shaders dont't compile.

private final String vertexShaderCode =
        // This matrix member variable provides a hook to manipulate
        // the coordinates of the objects that use this vertex shader
                "uniform mat4 uMVPMatrix;" +
                "in vec4 vPosition;" +
                "in vec2 vTextureCoordinate;" +
                "out vec2 exTextureCoordinate;" +
                "void main() {" +
                // the matrix must be included as a modifier of gl_Position
                // Note that the uMVPMatrix factor *must be first* in order
                // for the matrix multiplication product to be correct.
                "  gl_Position = uMVPMatrix * vPosition;" +
                "   exTextureCoordinate = vTextureCoordinate;" +
                "}";

private final String fragmentShaderCode =
                "precision mediump float;" +
                "uniform sampler2D texture;" +
                "in vec2 exTextureCoordinate;" +
                "out vec4 out_Color;" +
                "void main() {" +
                "  out_Color = texture2D(texture, exTextureCoordinate);" +
                "}";

Idea: pass a Model-View matrix to the vertexShader together with position in space and texture coordinate. The texture coordinate is passed through to the fragmentShader for coloring and the space coordinate is transformed with the Model-View Matrix.

I get following error code while compiling the shaders:

E/ShaderCompile: Error compiling shader!
E/ShaderCompile: 
ERROR: 0:1: 'in' : storage qualifier supported in GLSL ES 3.00 only  
ERROR: 0:1: 'in' : storage qualifier supported in GLSL ES 3.00 only  
ERROR: 0:1: 'out' : storage qualifier supported in GLSL ES 3.00 only  
E/ShaderCompile: Error compiling shader!
E/ShaderCompile: 
ERROR: 0:1: 'in' : storage qualifier supported in GLSL ES 3.00 only  
ERROR: 0:1: 'out' : storage qualifier supported in GLSL ES 3.00 only  

I use the following code to set the OpenGLES Version:

uses-feature android:glEsVersion="0x00030001" android:required="true"
setEGLContextClientVersion(3);

Solution

  • You don't define the version of GLSL to use, meaning it falls back to an earlier version (meaning not 3). You can use GLSL ES 1 with GLES 3, so it doesn't default to the current GLES version.

    So, at the top (the first line) of the shader file, declare the version:

    # version 300 es
    

    This has to be done for both the fragment and vertex shader


    Note that this actually requires the device to have support for GLES and GLSL 3. If you use the default Android emulator (the one that's packaged with the SDK) you can't test GLES/GLSLES 3, as it doesn't support it. Use a real device (with GLES3 support of course) or an emulator that supports GLES3