/*
   SimoneT mixed 3D - 2D games Shader v 2.1 + NTSC color emulation

   Copyright (C) 2018 SimoneT - simone1tarditi@gmail.com

   part of the code taken from "Directional Cubic Convolution Interpolation"
   created by Dengwen Zhou and Xiaoliu Shen.

   https://en.wikipedia.org/wiki/Directional_Cubic_Convolution_Interpolation

   de Blur - 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 3
   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.
    
*/

#version 130

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

const   vec2 Size1X  = vec2( 1024.0, 512.0 );
const   vec2 InvSize1X = vec2( 0.0009765625, 0.001953125 );

const   vec2 Size2X  = vec2( 2048.0, 1024.0 );
const   vec2 InvSize2X = vec2( 0.00048828125, 0.0009765625 );

const   vec2 Size4X  = vec2( 4096.0, 2048.0 );
const   vec2 InvSize4X = vec2( 0.000244140625, 0.00048828125 );

 	vec4 xy = vec4(InvSize2X,-InvSize2X);

vec3 ntscrgb_2_xyz( vec3 c ) {

    return pow( c, vec3(2.2) ) *
        mat3(  0.6068909,  0.1735011,  0.2003480,
 	       0.2989164,  0.5865990,  0.1144845,
 	       0.0000000,  0.0660957,  1.1162243 );
}

vec3 xyz_2_srgb( vec3 c ) {

    vec3 v =  c *         
	mat3(  3.2404542, -1.5371385, -0.4985314,
	      -0.9692660,  1.8760108,  0.0415560,
 	       0.0556434, -0.2040259,  1.0572252 );

    //Companding
    v.r = ( v.r > 0.0031308 ) ? (( 1.055 * pow( v.r, ( 1.0 / 2.4 ))) - 0.055 ) : 12.92 * v.r;
    v.g = ( v.g > 0.0031308 ) ? (( 1.055 * pow( v.g, ( 1.0 / 2.4 ))) - 0.055 ) : 12.92 * v.g;
    v.b = ( v.b > 0.0031308 ) ? (( 1.055 * pow( v.b, ( 1.0 / 2.4 ))) - 0.055 ) : 12.92 * v.b;
    return v;
}

// NTSC RGB to sRGB

vec3 ntscrgb_2_srgb( vec3 c ) { 
     return xyz_2_srgb( ntscrgb_2_xyz( c ) ); 
}


vec3 ColMin(vec3 a, vec3 b){

	float diff = step(0.0,sign(length(a)-length(b)));

	return mix(a,b,diff);

}

vec3 ColMax(vec3 a, vec3 b){

	float diff = step(0.0,sign(length(a)-length(b)));

	return mix(b,a,diff);

}
 
float detect3d( vec2 Coords ){

	vec2 dim = Coords * Size1X - 0.5;

	vec2 pos = floor(dim)*InvSize1X;

	vec3 a = texture(OGL2Texture, pos                           ).rgb;
	vec3 b = texture(OGL2Texture, pos + vec2( InvSize2X.x, 0.0 )).rgb;
	vec3 c = texture(OGL2Texture, pos + vec2( 0.0, InvSize2X.y )).rgb;
	vec3 d = texture(OGL2Texture, pos + InvSize2X               ).rgb; 

	float e = min(1.0,dot(abs(a-b)+abs(b-c)+abs(c-d),vec3(255.0)));

	return e;

}

vec3 Interpolate( vec3 a, vec3 b, vec3 c, vec3 d ) {

	return ( a + b ) * 0.4 + ( c + d ) * 0.1;

}

vec3 STAA( vec2 coord, vec2 Size, vec2 shift ) {

	vec2 dim = coord * Size;
   
	vec2 frac = fract(dim);

	vec2 TexCoord = floor(dim)*shift;

	vec2 shift01 = shift * 2.0;
	vec2 shift02 = vec2(   shift.x, shift01.y);
	vec2 shift03 = vec2(       0.0, shift01.y);
	vec2 shift04 = vec2(  -shift.x, shift01.y);
	vec2 shift05 = vec2(-shift01.x, shift01.y);
	vec2 shift06 = vec2( shift01.x,   shift.y);
	vec2 shift07 = vec2(       0.0,   shift.y);
	vec2 shift08 = vec2(  -shift.x,   shift.y);
	vec2 shift09 = vec2(-shift01.x,   shift.y);
	vec2 shift10 = vec2( shift01.x,       0.0);
	vec2 shift11 = vec2(   shift.x,       0.0);

	vec3 C00 = texture(OGL2Texture, TexCoord - shift01).rgb;
	vec3 C01 = texture(OGL2Texture, TexCoord - shift02).rgb;
	vec3 C02 = texture(OGL2Texture, TexCoord - shift03).rgb;
	vec3 C03 = texture(OGL2Texture, TexCoord - shift04).rgb;
	vec3 C04 = texture(OGL2Texture, TexCoord - shift05).rgb;

	vec3 C05 = texture(OGL2Texture, TexCoord - shift06).rgb;
	vec3 C06 = texture(OGL2Texture, TexCoord - shift  ).rgb;
	vec3 C07 = texture(OGL2Texture, TexCoord - shift07).rgb;
	vec3 C08 = texture(OGL2Texture, TexCoord - shift08).rgb;
	vec3 C09 = texture(OGL2Texture, TexCoord - shift09).rgb;

	vec3 C10 = texture(OGL2Texture, TexCoord - shift10).rgb;
	vec3 C11 = texture(OGL2Texture, TexCoord - shift11).rgb;
	vec3 C12 = texture(OGL2Texture, TexCoord          ).rgb;
	vec3 C13 = texture(OGL2Texture, TexCoord + shift11).rgb;
	vec3 C14 = texture(OGL2Texture, TexCoord + shift10).rgb;

	vec3 C15 = texture(OGL2Texture, TexCoord + shift09).rgb;
	vec3 C16 = texture(OGL2Texture, TexCoord + shift08).rgb;
	vec3 C17 = texture(OGL2Texture, TexCoord + shift07).rgb;
	vec3 C18 = texture(OGL2Texture, TexCoord + shift  ).rgb;
	vec3 C19 = texture(OGL2Texture, TexCoord + shift06).rgb;

	vec3 C20 = texture(OGL2Texture, TexCoord + shift05).rgb;
	vec3 C21 = texture(OGL2Texture, TexCoord + shift04).rgb;
	vec3 C22 = texture(OGL2Texture, TexCoord + shift03).rgb;
	vec3 C23 = texture(OGL2Texture, TexCoord + shift02).rgb;
	vec3 C24 = texture(OGL2Texture, TexCoord + shift01).rgb;

	float C03C09 = distance(C03,C09);
	float C02C08 = distance(C02,C08);
	float C08C14 = distance(C08,C14);
	float C01C07 = distance(C01,C07);
	float C07C13 = distance(C07,C13);
	float C13C19 = distance(C13,C19);
	float C00C06 = distance(C00,C06);
	float C06C12 = distance(C06,C12);
	float C12C18 = distance(C12,C18);
	float C18C24 = distance(C18,C24);
	float C05C11 = distance(C05,C11);
	float C11C17 = distance(C11,C17);
	float C17C23 = distance(C17,C23);
	float C10C16 = distance(C10,C16);
	float C16C22 = distance(C16,C22);
	float C15C21 = distance(C15,C21);

	vec4 d01 = vec4(
	C02C08 + C01C07 + C07C13 + C00C06 + C06C12 + C12C18 + C05C11 + C11C17 + C10C16,
	C03C09 + C02C08 + C08C14 + C01C07 + C07C13 + C13C19 + C06C12 + C12C18 + C11C17,
	C07C13 + C06C12 + C12C18 + C05C11 + C11C17 + C17C23 + C10C16 + C16C22 + C15C21,
	C08C14 + C07C13 + C13C19 + C06C12 + C12C18 + C18C24 + C11C17 + C17C23 + C16C22);

	float C01C05 = distance(C01,C05);
	float C02C06 = distance(C02,C06);
	float C06C10 = distance(C06,C10);
	float C03C07 = distance(C03,C07);
	float C07C11 = distance(C07,C11);
	float C11C15 = distance(C11,C15);
	float C04C08 = distance(C04,C08);
	float C08C12 = distance(C08,C12);
	float C12C16 = distance(C12,C16);
	float C16C20 = distance(C16,C20);
	float C09C13 = distance(C09,C13);
	float C13C17 = distance(C13,C17);
	float C17C21 = distance(C17,C21);
	float C14C18 = distance(C14,C18);
	float C18C22 = distance(C18,C22);
	float C19C23 = distance(C19,C23);

	vec4 d02 = vec4(
	C01C05 + C02C06 + C06C10 + C03C07 + C07C11 + C11C15 + C08C12 + C12C16 + C13C17,
	C02C06 + C03C07 + C07C11 + C04C08 + C08C12 + C12C16 + C09C13 + C13C17 + C14C18,
	C06C10 + C07C11 + C11C15 + C08C12 + C12C16 + C16C20 + C13C17 + C17C21 + C18C22,
	C07C11 + C08C12 + C12C16 + C09C13 + C13C17 + C17C21 + C14C18 + C18C22 + C19C23);

	vec4 minval = min(d01,d02);

	d01 = step(d01,minval);

	d02 = step(d02,minval);

	vec4 d03 = d01 + d02;

	d01 = d01/d03;

	d02 = d02/d03;

	vec3 DR0 = Interpolate( C06, C12, C00, C18 ); 
	vec3 UR0 = Interpolate( C07, C11, C03, C15 ); 

	vec3 DR1 = Interpolate( C07, C13, C01, C19 ); 
	vec3 UR1 = Interpolate( C08, C12, C04, C16 ); 

	vec3 DR2 = Interpolate( C11, C17, C05, C23 ); 
	vec3 UR2 = Interpolate( C12, C16, C08, C20 ); 

	vec3 DR3 = Interpolate( C12, C18, C06, C24 ); 
	vec3 UR3 = Interpolate( C13, C17, C09, C21 ); 

	vec3 sum0 = DR0 * d01.x + UR0 * d02.x; 
	vec3 sum1 = DR1 * d01.y + UR1 * d02.y;
	vec3 sum2 = DR2 * d01.z + UR2 * d02.z;
	vec3 sum3 = DR3 * d01.w + UR3 * d02.w;

	return mix(mix(sum0,sum1,frac.x),mix(sum2,sum3,frac.x),frac.y);

}

vec3 bilinear( vec2 Coords ) {

	vec2 dim = Coords * Size1X - 0.5;
   
	vec2 fxy = fract(dim);

	vec2 pos = floor(dim)*InvSize1X;

	vec2 posx  = pos+vec2( InvSize1X.x,         0.0);
	vec2 posy  = pos+vec2(         0.0, InvSize1X.y);
	vec2 posxy = pos+vec2( InvSize1X.x, InvSize1X.y);

	vec3 C0 = texture(OGL2Texture, pos   ).rgb; 
	vec3 C1 = texture(OGL2Texture, posx  ).rgb;
	vec3 C2 = texture(OGL2Texture, posy  ).rgb;
	vec3 C3 = texture(OGL2Texture, posxy ).rgb;

	vec3 C5 = mix(mix(C0,C1,fxy.x),mix(C2,C3,fxy.x),fxy.y);

	return C5;

}

//      de Blur code ( moddded by me... )

vec3 deBlur( vec2 texcoord, vec3 C4 ) {

	vec2 dx  = vec2(xy.x, 0.0);
	vec2 dy  = vec2( 0.0,xy.y);
	vec2 g1  = xy.xy;
	vec2 g2  = vec2(xy.z,xy.y);

	vec3 C0 = bilinear(texcoord-g1); 
	vec3 C1 = bilinear(texcoord-dy);
	vec3 C2 = bilinear(texcoord-g2);
	vec3 C3 = bilinear(texcoord-dx);
	vec3 C5 = bilinear(texcoord+dx);
	vec3 C6 = bilinear(texcoord+g2);
	vec3 C7 = bilinear(texcoord+dy);
	vec3 C8 = bilinear(texcoord+g1);

	vec3 mn1 = ColMin(ColMin(C0,C1),C2);
	vec3 mn2 = ColMin(ColMin(C3,C4),C5);
	vec3 mn3 = ColMin(ColMin(C6,C7),C8);
	     mn1 = ColMin(ColMin(mn1,mn2),mn3);

	vec3 mx1 = ColMax(ColMax(C0,C1),C2);
	vec3 mx2 = ColMax(ColMax(C3,C4),C5);
	vec3 mx3 = ColMax(ColMax(C6,C7),C8);
 	     mx1 = ColMax(ColMax(mx1,mx2),mx3);

	float dif1 = distance(C4,mn1);
	float dif2 = distance(C4,mx1);
 
	float filterparam = 7.0; // de Blur control

	dif1=pow(dif1,filterparam);
	dif2=pow(dif2,filterparam);

	float dif3=dif1+dif2; 

	vec3 C9 = (dif1*mx1 + dif2*mn1)/dif3;

	if (dif3 == 0.0 ) { C9 = C4; }
 
	return C9;

}

void main()
{


	vec2 posx  = gl_TexCoord[0].xy+vec2( InvSize1X.x,         0.0);
	vec2 posy  = gl_TexCoord[0].xy+vec2(         0.0, InvSize1X.y);
	vec2 posxy = gl_TexCoord[0].xy+vec2( InvSize1X.x, InvSize1X.y);

	float c0_3d = detect3d(gl_TexCoord[0].xy);
	float c1_3d = detect3d(posx);
	float c2_3d = detect3d(posy);
	float c3_3d = detect3d(posxy);

	float e = c0_3d+c1_3d+c2_3d+c3_3d;

	
	vec3 c0 = ( e < 1.0 || OGL2Size.x <= Size2X.x ) ? (deBlur(gl_TexCoord[0].xy,STAA(gl_TexCoord[0].xy,Size2X,InvSize2X))):
			        			  (STAA(gl_TexCoord[0].xy,OGL2Size.xy,OGL2InvSize.xy));

	gl_FragColor.rgb = ntscrgb_2_srgb(c0);
}
