/*
   AA shader 4.o - Level2 - CRT Lottes
   
   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.
*/


// 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.000              // x-curvature setting, 0.0 - 0.1
#define warpY 0.000              // 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;


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



// AA Shader 4.o - Level2 code

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


vec3 AA402x (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 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); 
}

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) );
	return 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;
}
