Cannot Access rgb properties of Vec4 in Fragment Shader OpenGL

Viewed 544

I have a simple fragment shader in which i pass two textures. I tested outputting both textures and they were correct (ie, what i inputed)

HOWEVER, when I try to access the rgb properties of a texture (converted into a vec4) the program gives an error of glerror 1282 (Invalid operation)

✘ The following shader crashes:

    #extension GL_OES_EGL_image_external : require
    precision mediump float;

    varying vec2 v_TexCoord;
    uniform sampler2D sTexture2;
    uniform samplerExternalOES sTexture;

    void main() {
        vec4 originalrgb = vec4((texture2D(sTexture, v_TexCoord).rgb), 1.0);
        vec4 floodfillimage = vec4((texture2D(sTexture2, v_TexCoord).rgb), 1.0);

        vec4 c = vec4(0.0, 1.0, 1.0, 1); /*Blue Tint Colour*/
        vec4 inputColor = vec4((texture2D(sTexture, v_TexCoord).rgb), 1.0);
        float average = (inputColor.r + inputColor.g + inputColor.b) / 3.0;
        vec4 grayscale = vec4(average, average, average, 1.0);
        vec4 colorizedOutput = grayscale * c;

        vec4 test;
        if(floodfillimage.r > 0.9){ /*This is what makes it crash, cant seem to access the rgb properties of this*/
            test = colorizedOutput.rgba;
        }else{
            test = originalrgb.rgba;
        }

        gl_FragColor = test;
    }

✔ This one does NOT Crash

    #extension GL_OES_EGL_image_external : require
    precision mediump float;
    /*
    * Original Camera Frame Image
    */
    varying vec2 v_TexCoord;
    uniform sampler2D sTexture2;
    uniform samplerExternalOES sTexture;
    void main() {
        vec4 originalrgb = vec4((texture2D(sTexture, v_TexCoord).rgb), 1.0);
        vec4 floodfillimage = vec4((texture2D(sTexture2, v_TexCoord).rgb), 1.0);

        vec4 c = vec4(0.0, 1.0, 1.0, 1); /*Blue Tint Colour*/
        vec4 inputColor = vec4((texture2D(sTexture, v_TexCoord).rgb), 1.0);
        float average = (inputColor.r + inputColor.g + inputColor.b) / 3.0;
        vec4 grayscale = vec4(average, average, average, 1.0);
        vec4 colorizedOutput = grayscale * c;

        gl_FragColor = floodfillimage;
    }

✔ This one does NOT Crash as well.

    #extension GL_OES_EGL_image_external : require
    precision mediump float;
    /*
    * Original Camera Frame Image
    */
    varying vec2 v_TexCoord;
    uniform sampler2D sTexture2;
    uniform samplerExternalOES sTexture;
    void main() {
        vec4 originalrgb = vec4((texture2D(sTexture, v_TexCoord).rgb), 1.0);
        vec4 floodfillimage = vec4((texture2D(sTexture2, v_TexCoord).rgb), 1.0);

        vec4 c = vec4(0.0, 1.0, 1.0, 1); /*Blue Tint Colour*/
        vec4 inputColor = vec4((texture2D(sTexture, v_TexCoord).rgb), 1.0);
        float average = (inputColor.r + inputColor.g + inputColor.b) / 3.0;
        vec4 grayscale = vec4(average, average, average, 1.0);
        vec4 colorizedOutput = grayscale * c;

        gl_FragColor = originalrgb;
    }

✔ This one does NOT Crash as well.

    #extension GL_OES_EGL_image_external : require
    precision mediump float;
    /*
    * Original Camera Frame Image
    */
    varying vec2 v_TexCoord;
    uniform sampler2D sTexture2;
    uniform samplerExternalOES sTexture;
    void main() {
        vec4 originalrgb = vec4((texture2D(sTexture, v_TexCoord).rgb), 1.0);
        vec4 floodfillimage = vec4((texture2D(sTexture2, v_TexCoord).rgb), 1.0);

        vec4 c = vec4(0.0, 1.0, 1.0, 1); /*Blue Tint Colour*/
        vec4 inputColor = vec4((texture2D(sTexture, v_TexCoord).rgb), 1.0);
        float average = (inputColor.r + inputColor.g + inputColor.b) / 3.0;
        vec4 grayscale = vec4(average, average, average, 1.0);
        vec4 colorizedOutput = grayscale * c;

        gl_FragColor = colorizedOutput;
    }

Why does it crash when I want to access the 'r', 'g' or 'b' properties of the texture?

Any help would be greatly appreciated!

EDIT Accessing the properties and assigning them directly to gl_Fragcolor works. ie, the following works:

gl_FragColor.r = floodfillimage.r;
gl_FragColor.g = floodfillimage.g;
gl_FragColor.b = floodfillimage.b;
gl_FragColor.a = floodfillimage.a;
0 Answers
Related