________________________
| Shader Created by : Unlimax |

}

uniform sampler2D OGL2Texture;

const float mx = 1.00;     // start smoothing wt.
const float k = -1.10;     // wt. decrease factor
const float max_w = 0.75;  // max filter weigth
const float min_w = 0.03;   // min filter weigth
const float lum_add = 0.33; // effects smoothing

void main()
{
	vec3 c  = texture2D(OGL2Texture, gl_TexCoord[0].xy).xyz;
	vec3 i1 = texture2D(OGL2Texture, gl_TexCoord[1].xy).xyz; 
	vec3 i2 = texture2D(OGL2Texture, gl_TexCoord[2].xy).xyz; 
	vec3 i3 = texture2D(OGL2Texture, gl_TexCoord[3].xy).xyz; 
	vec3 i4 = texture2D(OGL2Texture, gl_TexCoord[4].xy).xyz; 
	vec3 o1 = texture2D(OGL2Texture, gl_TexCoord[5].xy).xyz; 
	vec3 o3 = texture2D(OGL2Texture, gl_TexCoord[6].xy).xyz; 
	vec3 o2 = texture2D(OGL2Texture, gl_TexCoord[5].zw).xyz;
	vec3 o4 = texture2D(OGL2Texture, gl_TexCoord[6].zw).xyz;
	vec3 s1 = texture2D(OGL2Texture, gl_TexCoord[1].zw).xyz; 
	vec3 s2 = texture2D(OGL2Texture, gl_TexCoord[2].zw).xyz; 
	vec3 s3 = texture2D(OGL2Texture, gl_TexCoord[3].zw).xyz; 
	vec3 s4 = texture2D(OGL2Texture, gl_TexCoord[4].zw).xyz;  
	vec3 dt = vec3(1.0,1.0,1.0);

	float ko1=dot(abs(o1-c),dt);
	float ko2=dot(abs(o2-c),dt);
	float ko3=dot(abs(o3-c),dt);
	float ko4=dot(abs(o4-c),dt);

	float k1=min(dot(abs(i1-i3),dt),max(ko1,ko3));
	float k2=min(dot(abs(i2-i4),dt),max(ko2,ko4));

	float w1 = k2; if(ko3<ko1) w1*=ko3/ko1;
	float w2 = k1; if(ko4<ko2) w2*=ko4/ko2;
	float w3 = k2; if(ko1<ko3) w3*=ko1/ko3;
	float w4 = k1; if(ko2<ko4) w4*=ko2/ko4;

	c=(w1*o1+w2*o2+w3*o3+w4*o4+0.001*c)/(w1+w2+w3+w4+0.001);

	w1 = k*dot(abs(i1-c)+abs(i3-c),dt)/(0.125*dot(i1+i3,dt)+lum_add);
	w2 = k*dot(abs(i2-c)+abs(i4-c),dt)/(0.125*dot(i2+i4,dt)+lum_add);
	w3 = k*dot(abs(s1-c)+abs(s3-c),dt)/(0.125*dot(s1+s3,dt)+lum_add);
	w4 = k*dot(abs(s2-c)+abs(s4-c),dt)/(0.125*dot(s2+s4,dt)+lum_add);

	w1 = clamp(w1+mx,min_w,max_w); 
	w2 = clamp(w2+mx,min_w,max_w);
	w3 = clamp(w3+mx,min_w,max_w); 
	w4 = clamp(w4+mx,min_w,max_w);

	gl_FragColor.xyz=(w1*(i1+i3)+w2*(i2+i4)+w3*(s1+s3)+w4*(s2+s4)+c)/(2.0*(w1+w2+w3+w4)+1.0);
}

// Color variables

const vec3 c_ch = vec3(1.0,1.0,1.0);  //  rgb color channel intensity
const float   a = 1.60 ;              //  saturation 
const float   b = 1.60 ;              //  brightness 
const float   c = 1.00 ;              //  contrast    


uniform sampler2D OGL2Texture;

// The inverse of the texture dimensions along X and Y
uniform vec4 OGL2InvSize;

void main() {
  // The parameters are hardcoded for now, but could be
  // made into uniforms to control fromt he program.
  float FXAA_SPAN_MAX = 100.0;
  float FXAA_REDUCE_MUL = 12.0/8.0;
  float FXAA_REDUCE_MIN = (1.0/128.0);

  vec3 rgbNW = texture2D(OGL2Texture, gl_TexCoord[0].xy + (vec2(-1.0, -1.0) * OGL2InvSize)).xyz;
  vec3 rgbNE = texture2D(OGL2Texture, gl_TexCoord[0].xy + (vec2(+1.0, -1.0) * OGL2InvSize)).xyz;
  vec3 rgbSW = texture2D(OGL2Texture, gl_TexCoord[0].xy + (vec2(-1.0, +1.0) * OGL2InvSize)).xyz;
  vec3 rgbSE = texture2D(OGL2Texture, gl_TexCoord[0].xy + (vec2(+1.0, +1.0) * OGL2InvSize)).xyz;
  vec3 rgbM  = texture2D(OGL2Texture, gl_TexCoord[0].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 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN);
	  
  float rcpDirMin = 1.0/(min(abs(dir.x), abs(dir.y)) + dirReduce);
	
  dir = min(vec2(FXAA_SPAN_MAX,  FXAA_SPAN_MAX), 
        max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), dir * rcpDirMin)) * OGL2InvSize;
		
  vec3 rgbA = (1.0/2.0) * (
              texture2D(OGL2Texture, gl_TexCoord[0].xy + dir * (1.0/3.0 - 0.5)).xyz +
              texture2D(OGL2Texture, gl_TexCoord[0].xy + dir * (2.0/3.0 - 0.5)).xyz);
  vec3 rgbB = rgbA * (1.0/2.0) + (1.0/4.0) * (
              texture2D(OGL2Texture, gl_TexCoord[0].xy + dir * (0.0/3.0 - 0.5)).xyz +
              texture2D(OGL2Texture, gl_TexCoord[0].xy + dir * (3.0/3.0 - 0.5)).xyz);
  float lumaB = dot(rgbB, luma);

  if((lumaB < lumaMin) || (lumaB > lumaMax)){
    gl_FragColor.xyz=rgbA;
  } else {
    gl_FragColor.xyz=rgbB;
  }
  gl_FragColor.a = 1.0;
