/*
   AA shader 4.o - Level2 HQ (slow atm.)
   
   Copyright (C) 2016 guest(r) - guest.r@gmail.com

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/


const float offset = 1.0;    // Play with this value a bit. (from 0.25 -> 3D AA to 1.0 -> full sprite AA)


uniform sampler2D OGL2Texture;
uniform vec4 OGL2Param;

const vec3 dt = vec3(1.0,1.0,1.0);

vec4 yx = vec4(1.0/4096,1.0/2048,-1.0/4096,-1.0/2048)*offset;
vec4 xh = vec4(3.0/4096,1.0/2048,-3.0/4096,-1.0/2048)*offset;
vec4 yv = vec4(1.0/4096,3.0/2048,-1.0/4096,-3.0/2048)*offset;
float x = offset/2048.0;
float y = offset/1024.0;


vec3 ref (vec2 texcoord) {

	vec3 c   = texture2D(OGL2Texture, texcoord).xyz;
	vec3 i1  = texture2D(OGL2Texture, texcoord + yx.zw).xyz; 
	vec3 i2  = texture2D(OGL2Texture, texcoord + yx.xw).xyz; 
	vec3 i3  = texture2D(OGL2Texture, texcoord + yx.xy).xyz; 
	vec3 i4  = texture2D(OGL2Texture, texcoord + yx.zy).xyz; 
	vec3 o1  = texture2D(OGL2Texture, texcoord + vec2( -x, -y)).xyz; 
	vec3 o3  = texture2D(OGL2Texture, texcoord + vec2(  x,  y)).xyz; 
	vec3 o2  = texture2D(OGL2Texture, texcoord + vec2(  x, -y)).xyz;
	vec3 o4  = texture2D(OGL2Texture, texcoord + vec2( -x,  y)).xyz;

	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),dot(abs(o1-o3),dt));
	float k2=min(dot(abs(i2-i4),dt),dot(abs(o2-o4),dt));

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

	return (w1*o1+w2*o2+w3*o3+w4*o4+0.0001*c)/(w1+w2+w3+w4+0.0001);
}

vec3 texture2d (vec2 texcoord) {

	vec3 c11 = ref( texcoord        ).xyz; 
	vec3 s00 = texture2D(OGL2Texture, texcoord + yx.zw).xyz; 
	vec3 s20 = texture2D(OGL2Texture, texcoord + yx.xw).xyz; 
	vec3 s22 = texture2D(OGL2Texture, texcoord + yx.xy).xyz; 
	vec3 s02 = texture2D(OGL2Texture, texcoord + yx.zy).xyz; 

	vec3 h00 = texture2D(OGL2Texture, texcoord + xh.zw).xyz; 
	vec3 h20 = texture2D(OGL2Texture, texcoord + xh.xw).xyz; 
	vec3 h22 = texture2D(OGL2Texture, texcoord + xh.xy).xyz; 
	vec3 h02 = texture2D(OGL2Texture, texcoord + xh.zy).xyz; 

	vec3 v00 = texture2D(OGL2Texture, texcoord + yv.zw).xyz; 
	vec3 v20 = texture2D(OGL2Texture, texcoord + yv.xw).xyz; 
	vec3 v22 = texture2D(OGL2Texture, texcoord + yv.xy).xyz; 
	vec3 v02 = texture2D(OGL2Texture, texcoord + yv.zy).xyz; 

	float m1=1.0/(dot(abs(s00-s22),dt)+0.00001);
	float m2=1.0/(dot(abs(s02-s20),dt)+0.00001);

	float h1=1.0/(dot(abs(s00-h22),dt)+0.00001);
	float h2=1.0/(dot(abs(s02-h20),dt)+0.00001);

	float h3=1.0/(dot(abs(h00-s22),dt)+0.00001);
	float h4=1.0/(dot(abs(h02-s20),dt)+0.00001);

	float v1=1.0/(dot(abs(s00-v22),dt)+0.00001);
	float v2=1.0/(dot(abs(s02-v20),dt)+0.00001);

	float v3=1.0/(dot(abs(v00-s22),dt)+0.00001);
	float v4=1.0/(dot(abs(v02-s20),dt)+0.00001);

	vec3 t1 = 0.5*(m1*(s00+s22)+m2*(s02+s20))/(m1+m2);
	vec3 t2 = 0.5*(h1*(s00+h22)+h2*(s02+h20)+h3*(h00+s22)+h4*(h02+s20))/(h1+h2+h3+h4);
	vec3 t3 = 0.5*(v1*(s00+v22)+v2*(s02+v20)+v3*(v00+s22)+v4*(v02+s20))/(v1+v2+v3+v4);

	float k1 = 1.0/(dot(abs(t1-c11),dt)+0.00001);
	float k2 = 1.0/(dot(abs(t2-c11),dt)+0.00001);
	float k3 = 1.0/(dot(abs(t3-c11),dt)+0.00001);

	return (k1*t1 + k2*t2 + k3*t3)/(k1+k2+k3); 
}

void main()
{
	// Calculating texel coordinates
	vec2 size     = vec2(4096.0,2048.0)/offset;
	vec2 inv_size = vec2(1.0/4096.0,1.0/2048.0)*offset;
	
	vec2 OGL2Pos = gl_TexCoord[0].xy * size;
	vec2 fp = fract(OGL2Pos);
	vec2 dx = vec2(inv_size.x,0.0);
	vec2 dy = vec2(0.0, inv_size.y);
	vec2 g1 = vec2(inv_size.x,inv_size.y);
	vec2 g2 = vec2(-inv_size.x,inv_size.y);
	
	vec2 pC4 = floor(OGL2Pos) * inv_size + 0.5*inv_size;	
	
	// Reading the texels
	vec3 C0 = texture2d(pC4 - g1); 
	vec3 C1 = texture2d(pC4 - dy);
	vec3 C2 = texture2d(pC4 - g2);
	vec3 C3 = texture2d(pC4 - dx);
	vec3 C4 = texture2d(pC4     );
	vec3 C5 = texture2d(pC4 + dx);
	vec3 C6 = texture2d(pC4 + g2);
	vec3 C7 = texture2d(pC4 + dy);
	vec3 C8 = texture2d(pC4 + g1);
	
	vec3 ul, ur, dl, dr;
	float m1, m2;
	
	m1 = dot(abs(C0-C4),dt)+0.001;
	m2 = dot(abs(C1-C3),dt)+0.001;
	ul = (m2*(C0+C4)+m1*(C1+C3))/(m1+m2);  
	
	m1 = dot(abs(C1-C5),dt)+0.001;
	m2 = dot(abs(C2-C4),dt)+0.001;
	ur = (m2*(C1+C5)+m1*(C2+C4))/(m1+m2);
	
	m1 = dot(abs(C3-C7),dt)+0.001;
	m2 = dot(abs(C6-C4),dt)+0.001;
	dl = (m2*(C3+C7)+m1*(C6+C4))/(m1+m2);
	
	m1 = dot(abs(C4-C8),dt)+0.001;
	m2 = dot(abs(C5-C7),dt)+0.001;
	dr = (m2*(C4+C8)+m1*(C5+C7))/(m1+m2);
	
	vec3 c11 = 0.5*((dr*fp.x+dl*(1-fp.x))*fp.y+(ur*fp.x+ul*(1-fp.x))*(1-fp.y));
 
	gl_FragColor.xyz = c11;
}
