Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 880
Mad Scientist Avatar asked Sep 07 '13 11:09

Mad Scientist


People also ask

How do I check my OpenGL version on Android?

How can I check the OpenGL version of my Android device? You can search the internet for information about the capabilities of the contained graphics adapter (search term: ModelOfYourDevice OpenGL).

Can I use OpenGL in android?

The basics. Android supports OpenGL both through its framework API and the Native Development Kit (NDK).

Can I update OpenGL version Android?

You can't. The OpenGL version supported depends on the hardware capabilities of your GPU. Outdated GPUs such as the Adreno 200 and Mali-400 series do not support version 3.0.


1 Answers

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.

like image 151
umlum Avatar answered Sep 22 '22 07:09

umlum