Poperton
Poperton

Reputation: 2127

Simple android shaders compile but linking fails

I'm trying to make these 2 shaders work. I even made the fragment shader display a red color instead of the RGB data, just to see if they're working. However linking process fails.

public static final String videoVertex =
    "#version 300 es\n"+
    "precision mediump float;\n"+
    "layout (location = 0) in vec3 aPos;\n" +
    "layout (location = 1) in vec2 aTexCoord;\n" +
    "\n" +
    "out vec2 TexCoord;\n" +
    "\n" +
    "void main()\n" +
    "{\n" +
    "    gl_Position = vec4(aPos, 1.0);\n" +
    "    TexCoord = vec2(aTexCoord.x, aTexCoord.y);\n" +
    "}\n";

public static final String videoFragment =
    "#version 300 es\n"+
    "uniform float alpha;\n" +
    "uniform sampler2D sampler;\n"+
    "\n" +
    "in vec2 TexCoord;\n" +
    "out vec4 FragColor;\n" +
    "void main()\n" +
    "{\n" +
    "    vec4 rgba;\n" +
    "    rgba.rgb = texture(sampler, TexCoord).rgb;\n" +
    "    rgba.a = 1;\n" +
    "    FragColor = vec4(1.0f,0,0,1.0f);\n" +
    "    //FragColor = rgba;\n" +
    "}\n";

mProgram = GLES20.glCreateProgram();

int vertexShader = SimpleGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER, videoVertex);
int fragmentShader = SimpleGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, videoFragment);

int[] compiled = new int[1];

GLES20.glAttachShader(mProgram, vertexShader);
GLES20.glGetShaderiv(vertexShader, GLES20.GL_COMPILE_STATUS, compiled, 0);
Log.d(TAG, "### vertex shader compilation: " + Arrays.toString(compiled));

GLES20.glAttachShader(mProgram, fragmentShader);
GLES20.glGetShaderiv(vertexShader, GLES20.GL_COMPILE_STATUS, compiled, 0);
Log.d(TAG, "### fragment shader compilation: " + Arrays.toString(compiled));

GLES20.glLinkProgram(mProgram);
int[] link = new int[1];
GLES20.glGetProgramiv(mProgram, GLES20.GL_LINK_STATUS, link, 0);
if (link[0] <= 0) {
    Log.d("### Load Program", "Linking Failed: " + link[0]);
}

This is the output I get:

2020-07-11 01:57:17.318 22963-23004/com.example.mediacodecdecoderexample D/VideoRenderer: ### vertex shader compilation: [1]
2020-07-11 01:57:17.318 22963-23004/com.example.mediacodecdecoderexample D/VideoRenderer: ### fragment shader compilation: [1]
2020-07-11 01:57:17.318 22963-23004/com.example.mediacodecdecoderexample D/### Load Program: Linking Failed: 0

As you can see, compilation went well, but not linking. How can this be possible?

Upvotes: 1

Views: 286

Answers (1)

Rabbid76
Rabbid76

Reputation: 210878

Your fragment shader does not compile, because in OpenGL ES 3.0 it is not allowed to assign an integral constant to a floating point variable. You have to assign a floating point:

rgba.a = 1;

rgba.a = 1.0;

Further more the precision qualifier for floating point types is missing. FOr instance:

precision mediump float; 

See OpenGL ES Shading Language 3.00 Specification - 4.5.4 Default Precision Qualifiers:

The fragment language has no default precision qualifier for floating point types. Hence for float, floating point vector and matrix variable declarations, either the declaration must include a precision qualifier or the default float precision must have been previously declared.

Upvotes: 1

Related Questions