//##############################################################//
//						                //
//       AA Shader 4.o + colors shader - coded by guest(r)      //
//               (Includes CRT-Lottes effects)                  //
//		     part of code by ShadX		        //
//##############################################################// 



// Note: the gpuPeteOGL2_t1l.tga file must be copied together with the shader files in order for the shader to work



// CRT-Lottes settings (editable)

#define shadowMask 1             // 1, 2, 3 or 4 (CRT style)
#define maskDark 0.5             // Dark shadowmask value  
#define maskLight 1.5            // Light shadowmask value
#define warpX 0.041              // x-curvature setting, 0.0 - 0.1
#define warpY 0.031              // y-curvature setting, 0.0 - 0.1
#define scaleInLinearGamma 1     // 0 or 1 (0 dark, 1 bright), adjust maskDark and maskLight if you change this


uniform sampler2D OGL2Texture;
uniform vec4 OGL2Size;
uniform vec4 OGL2Param;
uniform vec4 OGL2InvSize;

float bloom   = 0.50;
float natural = 1.15;


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.000, 1.000, 1.000,
                           0.955,-0.271,-1.108,
                           0.619,-0.646, 1.705);



// AA Shader 4.o code

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

vec3 AA402x (vec2 texcoord) {

	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; 

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

	return .5*(m2*(s00+s22)+m1*(s02+s20))/(m1+m2);
}

vec3 AA40 (sampler2D OGL2Texture, vec2 texcoord) {
	
	// Calculating texel coordinates
	vec2 size     = vec2(4096.0,2048.0);
	vec2 inv_size = vec2(1.0/4096.0,1.0/2048.0);
	
	vec2 OGL2Pos = texcoord * 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 = AA402x(pC4 - g1); 
	vec3 C1 = AA402x(pC4 - dy);
	vec3 C2 = AA402x(pC4 - g2);
	vec3 C3 = AA402x(pC4 - dx);
	vec3 C4 = AA402x(pC4     );
	vec3 C5 = AA402x(pC4 + dx);
	vec3 C6 = AA402x(pC4 + g2);
	vec3 C7 = AA402x(pC4 + dy);
	vec3 C8 = AA402x(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) );

	if  (OGL2Param.z < 2.0) natural = 1.0;
	c11=RGBtoYIQ*c11;
	c11=vec3(pow(c11.x,natural),c11.yz*natural);
	c11=YIQtoRGB*c11;
	float k1 = length(c11); bloom = mod(OGL2Param.z,2)*bloom;
	return (1.0-bloom)*c11 + bloom*mix(k1,normalize(vec2(k1*k1,0.82)).x*1.74,0.577*k1)*normalize(c11);
}

// CRT-Lottes code

#define warp vec2(warpX,warpY)

float ToLinear1(float c)
{
   if (scaleInLinearGamma==0.0) return c;
   return(c<=0.04045)?c/12.92:pow((c+0.055)/1.055,2.4);
}
vec3 ToLinear(vec3 c)
{
   if (scaleInLinearGamma==0.0) return c;
   return vec3(ToLinear1(c.r),ToLinear1(c.g),ToLinear1(c.b));
}

// Linear to sRGB.
// Assuming using sRGB typed textures this should not be needed.

float ToSrgb1(float c)
{
   if (scaleInLinearGamma==0.0) return c;
   return(c<0.0031308?c*12.92:1.055*pow(c,0.41666)-0.055);
}

vec3 ToSrgb(vec3 c)
{
   if (scaleInLinearGamma==0.0) return c;
   return vec3(ToSrgb1(c.r),ToSrgb1(c.g),ToSrgb1(c.b));
}


// Distortion of scanlines, and end of screen alpha.
vec2 Warp(vec2 pos){
  pos=pos*2.0-1.0;    
  pos*=vec2(1.0+(pos.y*pos.y)*warp.x,1.0+(pos.x*pos.x)*warp.y);
  return pos*0.5+0.5;}

// Shadow mask 
vec3 Mask(vec2 pos){
  vec3 mask=vec3(maskDark,maskDark,maskDark);

  // Very compressed TV style shadow mask.
  if (shadowMask == 1) {
    float line=maskLight;
    float odd=0.0;
    if(fract(pos.x/6.0)<0.5)odd=1.0;
    if(fract((pos.y+odd)/2.0)<0.5)line=maskDark;  
    pos.x=fract(pos.x/3.0);
   
    if(pos.x<0.333)mask.r=maskLight;
    else if(pos.x<0.666)mask.g=maskLight;
    else mask.b=maskLight;
    mask*=line;  
  } 

  // Aperture-grille.
  else if (shadowMask == 2) {
    pos.x=fract(pos.x/3.0);

    if(pos.x<0.333)mask.r=maskLight;
    else if(pos.x<0.666)mask.g=maskLight;
    else mask.b=maskLight;
  } 

  // Stretched VGA style shadow mask (same as prior shaders).
  else if (shadowMask == 3) {
    pos.x+=pos.y*3.0;
    pos.x=fract(pos.x/6.0);

    if(pos.x<0.333)mask.r=maskLight;
    else if(pos.x<0.666)mask.g=maskLight;
    else mask.b=maskLight;
  }

  // VGA style shadow mask.
  else if (shadowMask == 4) {
    pos.xy=floor(pos.xy*vec2(1.0,0.5));
    pos.x+=pos.y*3.0;
    pos.x=fract(pos.x/6.0);

    if(pos.x<0.333)mask.r=maskLight;
    else if(pos.x<0.666)mask.g=maskLight;
    else mask.b=maskLight;
  }

  return mask;
  }    

void main()
{
	// OGL2 tweaks by guest.r
	vec2 texcoord = vec2(gl_TexCoord[1].x,1.0-gl_TexCoord[1].y);
	vec2 pos=Warp(texcoord)*OGL2Size.zw;
	vec2 diff = pos - texcoord*OGL2Size.zw;
	
	float black = 1.0 - sign(dot(abs(pos-clamp(pos,vec2(OGL2InvSize.x, OGL2InvSize.y),vec2(OGL2Size.z-OGL2InvSize.x,OGL2Size.w-OGL2InvSize.y))),vec2(1.0)));  // garbage cleaning

	vec3 color = ToLinear(AA40(OGL2Texture, gl_TexCoord[0].xy+diff).xyz);
	color*= Mask(gl_FragCoord.xy); // Tweak by SimoneT

	gl_FragColor.xyz = ToSrgb(color)*black;
}
