/*
   AA + BumpMapping + NV HQ shader
   
   by 2015 guest(r) - guest.r@gmail.com

   Natural part of code by ShadX	
*/

/* Shader level:
	- 1 - light  AA + bump
	- 2 - light  AA + bump
	- 3 - strong AA + bump
	- 4 - strong AA + bump
*/	



uniform sampler2D OGL2Texture;
uniform vec4 OGL2Param;


// bump mapping parameters
const float glow = 1.25;
const float shde = 0.75;


// bump mapping - shader level
const float[4] bump = float[4](1.10,0.75,1.80,1.50);



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

const mat3 RGBtoYIQ = mat3(0.299, 0.596, 0.212, 
                           0.587,-0.275,-0.523, 
                           0.114,-0.321, 0.311);

const mat3 YIQtoRGB = mat3(1.0, 1.0, 1.0,
                           0.9556,-0.2715,-1.1081,
                           0.6198,-0.6468, 1.7050);

const vec3 val00 = vec3( 1.2, 1.2, 1.2);


float offset = 0.25 + (int(OGL2Param.z)/2)*0.75;

float x = offset/2048.0;
float y = offset/1024.0;


vec4 yx = vec4(1.0/4096,1.0/2048,-1.0/4096,-1.0/2048)*offset;

vec3 texture2d (vec2 texcoord) {

	vec3 c11 = texture2D(OGL2Texture, 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 c00 = texture2D(OGL2Texture, texcoord + vec2( -x, -y)).xyz; 
	vec3 c22 = texture2D(OGL2Texture, texcoord + vec2(  x,  y)).xyz; 
	vec3 c20 = texture2D(OGL2Texture, texcoord + vec2(  x, -y)).xyz;
	vec3 c02 = texture2D(OGL2Texture, texcoord + vec2( -x,  y)).xyz;
	vec3 c10 = texture2D(OGL2Texture, texcoord + vec2(0.0, -y)).xyz; 
	vec3 c21 = texture2D(OGL2Texture, texcoord + vec2(  x,0.0)).xyz; 
	vec3 c12 = texture2D(OGL2Texture, texcoord + vec2(0.0,  y)).xyz; 
	vec3 c01 = texture2D(OGL2Texture, texcoord + vec2( -x,0.0)).xyz;     

	float d1=dot(abs(c00-c22),dt)+0.001;
	float d2=dot(abs(c20-c02),dt)+0.001;
	float hl=dot(abs(c01-c21),dt)+0.001;
	float vl=dot(abs(c10-c12),dt)+0.001;
	float m1=dot(abs(s00-s22),dt)+0.001;
	float m2=dot(abs(s02-s20),dt)+0.001;

	vec3 t1=(hl*(c10+c12)+vl*(c01+c21)+(hl+vl)*c11)/(3.0*(hl+vl));
	vec3 t2=(d1*(c20+c02)+d2*(c00+c22)+(d1+d2)*c11)/(3.0*(d1+d2));
	
	c11 =.25*(t1+t2+(m2*(s00+s22)+m1*(s02+s20))/(m1+m2));

	return c11; 
}


void main()
{
    vec3 c00 = texture2d(gl_TexCoord[5].xy); 
    vec3 c10 = texture2d(gl_TexCoord[1].xy); 
    vec3 c20 = texture2d(gl_TexCoord[2].zw); 
    vec3 c01 = texture2d(gl_TexCoord[3].xy); 
    vec3 c11 = texture2d(gl_TexCoord[0].xy); 
    vec3 c21 = texture2d(gl_TexCoord[4].xy); 
    vec3 c02 = texture2d(gl_TexCoord[1].zw); 
    vec3 c12 = texture2d(gl_TexCoord[2].xy); 
    vec3 c22 = texture2d(gl_TexCoord[6].xy);       
    vec3 d11 = c11;
    
float bump_param;

    bump_param = bump[int(OGL2Param.z)];
    
    c11 = (-c00+c22-c01+c21-c10+c12+bump_param*d11)/bump_param;
    c11 = min(c11,glow*d11);
    c11 = max(c11,shde*d11);

    c11=RGBtoYIQ*c11;
    c11=vec3(pow(c11.x,val00.x),c11.yz*val00.yz);
    c11=YIQtoRGB*c11;

    gl_FragColor.xyz=c11*1.05;
}