precision mediump float;

//------------------------------------------------------------------------------------------------//

const int formatRGBA           = 0;
const int formatBGRA           = 1;
const int formatYUV            = 2;
const int formatYUVInterleaved = 3;

//------------------------------------------------------------------------------------------------//

uniform sampler2D inRGBTexture;
uniform sampler2D inYTexture;
uniform sampler2D inUTexture;
uniform sampler2D inVTexture;
uniform sampler2D inUVTexture;
uniform bool      inUVInterleaved;
uniform int       inFormat;
uniform vec2      inResolution;
uniform float     inTransparencyEffectFactor;
uniform float     inBrightnessEffectFactor;
uniform bool      inHorizontalBlurEffect;
uniform bool      inVerticalBlurEffect;
uniform bool      inSepiaEffect;
uniform float     inBlurKernel[10];
varying vec2      varTexturePosition;

//------------------------------------------------------------------------------------------------//

void main(void) {
    if (inFormat == formatRGBA) {
        gl_FragColor = texture2D(inRGBTexture, varTexturePosition).rgba;
    }
    else if (inFormat == formatBGRA) {
        gl_FragColor = texture2D(inRGBTexture, varTexturePosition).bgra;
    }
    else if (inFormat == formatYUV) {
        float y, u, v, r, g, b;

        y = texture2D(inYTexture, varTexturePosition).r;
        u = texture2D(inUTexture, varTexturePosition).r - 0.5;
        v = texture2D(inVTexture, varTexturePosition).r - 0.5;

        r = y + 1.370705 * v;
        g = y - 0.337633 * u - 0.698001 * v;
        b = y + 1.732446 * u;

        gl_FragColor = vec4(r, g, b, 1.0);
    }
    else if (inFormat == formatYUVInterleaved) {
        float y, u, v, r, g, b;

        y = texture2D(inYTexture,  varTexturePosition).r;
        u = texture2D(inUVTexture, varTexturePosition).a - 0.5;
        v = texture2D(inUVTexture, varTexturePosition).r - 0.5;

        r = y + 1.370705 * v;
        g = y - 0.337633 * u - 0.698001 * v;
        b = y + 1.732446 * u;

        gl_FragColor = vec4(r, g, b, 1.0);
    }

    if (inHorizontalBlurEffect || inVerticalBlurEffect) {
        vec4  color      = vec4(0.);
        int   kernelSize = int(inBlurKernel[0]);
        float shift      = 0.;
        float shiftX     = 0.;
        float shiftY     = 0.;

        color += texture2D(inRGBTexture, vec2(varTexturePosition.x, varTexturePosition.y)) * inBlurKernel[1];

        for (int i = 1; i <= kernelSize; i++) {
            shift  = inResolution.x * float(i);
            shiftX = inHorizontalBlurEffect ? (inResolution.x * float(i)) : 0.;
            shiftY = inVerticalBlurEffect   ? (inResolution.y * float(i)) : 0.;
            color += texture2D(inRGBTexture, vec2(varTexturePosition.x - shiftX, varTexturePosition.y - shiftY)) * inBlurKernel[i + 1];
            color += texture2D(inRGBTexture, vec2(varTexturePosition.x + shiftX, varTexturePosition.y + shiftY)) * inBlurKernel[i + 1];
        }

        if (inFormat == 0) {
            gl_FragColor = color.rgba;
        }
        else if (inFormat == 1) {
            gl_FragColor = color.bgra;
        }
    }

    if (inSepiaEffect) {
        const vec3 GREY_WEIGHTS = vec3(0.299, 0.587, 0.114);
        const vec3 SEPIA_WIGHTS = vec3(1.0, 0.8, 0.6);

        float grayColor  = dot(gl_FragColor.rgb, GREY_WEIGHTS);
        vec3  sepiaColor = vec3(grayColor) * SEPIA_WIGHTS;

        gl_FragColor = vec4(mix(gl_FragColor.rgb, sepiaColor, 0.5), gl_FragColor.a);
    }

    if (inBrightnessEffectFactor != 0.) {
        gl_FragColor = vec4(gl_FragColor.rgb + vec3(inBrightnessEffectFactor, inBrightnessEffectFactor, inBrightnessEffectFactor), gl_FragColor.a);
    }

    if (inTransparencyEffectFactor != 0.) {
        gl_FragColor = mix(gl_FragColor.rgba, vec4(gl_FragColor.rgb, (1. - inTransparencyEffectFactor)), 1.);
    }
}