/*
	Global algorithm - XPeng
*/

#version 130
precision mediump float;

uniform sampler2D SamplerBGRA;
uniform sampler2D SamplerTableTex1;
uniform sampler2D SamplerTableTex2;
uniform sampler2D SamplerBackTex1;
//uniform sampler2D SamplerBackTex2;

out     vec4 		outText;	// output sampler

void main(void)
{
    mediump vec3 dst;
    mediump vec3 dst_a;
    mediump vec3 dst_b;
    mediump vec4 txt;
    mediump float lum;
    
    dst = texture2D(SamplerBGRA, gl_TexCoord[0].st).rgb;        
    txt = texture2D(SamplerBackTex1, gl_TexCoord[0].st);
    lum = dst.r*0.299 + dst.g*0.587 + dst.b*0.114;
    dst = dst*0.5 + lum*0.5;
    
    dst_a.r = texture2D(SamplerTableTex1, vec2(dst.r,0.0)).r;
    dst_a.g = texture2D(SamplerTableTex1, vec2(dst.g,0.0)).g;
    dst_a.b = texture2D(SamplerTableTex1, vec2(dst.b,0.0)).b;
    
    dst_b.r = texture2D(SamplerTableTex2, vec2(dst.r,0.0)).r;
    dst_b.g = texture2D(SamplerTableTex2, vec2(dst.g,0.0)).g;
    dst_b.b = texture2D(SamplerTableTex2, vec2(dst.b,0.0)).b;
    
    dst = dst_a * (1.0 - txt.g) + dst_b * txt.g;
    outText = vec4(dst,1.0);
}