#if __VERSION__ >= 300
# define IN in
# define OUT out
#elif __VERSION__ >= 100
# define IN attribute
# define OUT varying
#endif

IN vec2 a_position;
IN vec2 a_uv;
IN vec4 a_tint;

OUT vec2 v_uv;
OUT vec4 v_tint;
OUT vec4 v_posPos;

uniform mat3 u_projection;
uniform vec2 u_destSize;

uniform float u_fxaaSubPixelShift;

void main() {
	gl_Position = vec4(u_projection * vec3(a_position.xy, 1.0), 1.0);
	v_uv = a_uv;
	v_tint = a_tint;

	vec2 rcpFrame = vec2(1.0/u_destSize.x, 1.0/u_destSize.y);
	v_posPos.xy = a_uv;
	v_posPos.zw = a_uv - (rcpFrame * (0.5 + u_fxaaSubPixelShift));
}

##VERTEX_END

precision highp float;

#if __VERSION__ >= 300
# define IN in
# define OUT out
# define TEXTURE2D texture
#elif __VERSION__ >= 100
# define IN varying
# define OUT
# define TEXTURE2D texture2D
#endif

IN vec2 v_uv;
IN vec4 v_tint;
IN vec4 v_posPos;

uniform sampler2D u_texture;
uniform vec2 u_destSize;

// uniform int u_showEdges;
uniform int u_fxaaOn;

uniform float u_fxaaSpanMax;
uniform float u_fxaaReduceMul;

OUT vec4 fragColor;

#if __VERSION__ >= 300
#
#define FxaaInt2 ivec2
#define FxaaFloat2 vec2

vec3 FxaaPixelShader( 
	vec4 posPos, // Output of FxaaVertexShader interpolated across screen.
	sampler2D tex, // Input texture.
	vec2 rcpFrame) // Constant {1.0/frameWidth, 1.0/frameHeight}.
{
	/*---------------------------------------------------------*/
#define FXAA_REDUCE_MIN   (1.0/128.0)
	//#define FXAA_REDUCE_MUL   (1.0/8.0)
	//#define u_fxaaSpanMax     8.0
	/*---------------------------------------------------------*/
	vec3 rgbNW = TEXTURE2D(tex, posPos.zw).xyz;
	vec3 rgbNE = textureOffset(tex, vec2(posPos.z, posPos.w), ivec2(1,0)).xyz;
	vec3 rgbSW = textureOffset(tex, posPos.zw, FxaaInt2(0,1)).xyz;
	vec3 rgbSE = textureOffset(tex, posPos.zw, FxaaInt2(1,1)).xyz;
	vec3 rgbM  = TEXTURE2D(tex, posPos.xy).xyz;
	/*---------------------------------------------------------*/
	vec3 luma = vec3(0.299, 0.587, 0.114);
	float lumaNW = dot(rgbNW, luma);
	float lumaNE = dot(rgbNE, luma);
	float lumaSW = dot(rgbSW, luma);
	float lumaSE = dot(rgbSE, luma);
	float lumaM  = dot(rgbM,  luma);
	/*---------------------------------------------------------*/
	float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
	float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
	/*---------------------------------------------------------*/
	vec2 dir; 
	dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
	dir.y =  ((lumaNW + lumaSW) - (lumaNE + lumaSE));
	/*---------------------------------------------------------*/
	float dirReduce = max(
		(lumaNW + lumaNE + lumaSW + lumaSE) * (0.25 * u_fxaaReduceMul),
		FXAA_REDUCE_MIN);
	float rcpDirMin = 1.0/(min(abs(dir.x), abs(dir.y)) + dirReduce);
	dir = min(FxaaFloat2( u_fxaaSpanMax,  u_fxaaSpanMax), 
		max(FxaaFloat2(-u_fxaaSpanMax, -u_fxaaSpanMax), 
			dir * rcpDirMin)) * rcpFrame.xy;
	/*--------------------------------------------------------*/
	vec3 rgbA = (1.0/2.0) * (
		TEXTURE2D(tex, posPos.xy + dir * (1.0/3.0 - 0.5)).xyz +
		TEXTURE2D(tex, posPos.xy + dir * (2.0/3.0 - 0.5)).xyz
	);

	vec3 rgbB = rgbA * (1.0/2.0) + (1.0/4.0) * (
		TEXTURE2D(tex, posPos.xy + dir * (0.0/3.0 - 0.5)).xyz +
		TEXTURE2D(tex, posPos.xy + dir * (3.0/3.0 - 0.5)).xyz
	);

	float lumaB = dot(rgbB, luma);
	if((lumaB < lumaMin) || (lumaB > lumaMax)) return rgbA;
	return rgbB;
}

vec4 PostFX(sampler2D tex, vec2 uv, float time) {
	vec4 c = vec4(0.0);
	vec2 rcpFrame = vec2(1.0/u_destSize.x, 1.0/u_destSize.y);
	c.rgb = FxaaPixelShader(v_posPos, tex, rcpFrame);
	//c.rgb = 1.0 - texture2D(tex, posPos.xy).rgb;
	c.a = TEXTURE2D(tex, uv).a;
	return c;
}
#endif

void main(void) { 
#if __VERSION__ >= 300
	if (u_fxaaOn == 1) {
		fragColor = PostFX(u_texture, v_uv, 0.0) / v_tint;
	} else {
		vec4 tex = vec4(TEXTURE2D(u_texture, v_uv));
		// fragColor = mix(tex, v_tint, v_tint.a);
		fragColor = tex / v_tint;
	}
#elif __VERSION__ >= 100
	vec4 tex = vec4(TEXTURE2D(u_texture, v_uv));
	fragColor = tex / v_tint;
#endif

#if __VERSION__ == 100
	gl_FragColor = fragColor;
#endif
}
