/*
   SimoneTpure 2D games Shader v 1.16 WIP Fast version

   Copyright (C) 2016-2017 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, 4xGLSoft lite - 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.
    
*/

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

const   vec2 Size1D    = vec2( 1024.0, 512.0 );
const   vec2 InvSize1D = vec2(0.0009765625,0.001953125);
const   vec4 Size2D = vec4( 2048.0, 1024.0, 0.00048828125  , 0.0009765625   );
const   vec4 Size3D = vec4( 8192.0, 4096.0, 0.0001220703125, 0.000244140625 );

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

const	vec4 exp = vec4(30.0);	

const	vec4 yx = vec4( 0.000244140625, 0.00048828125,-0.000244140625,-0.00048828125);
const	vec4 xy = vec4( 0.00048828125 , 0.0009765625 ,-0.00048828125 ,-0.0009765625 );

const   vec2 dx  = vec2( 0.00048828125, 0.0         );
const   vec2 dy  = vec2( 0.0          , 0.0009765625);
const   vec2 g1  = vec2( 0.00048828125, 0.0009765625);
const   vec2 g2  = vec2(-0.00048828125, 0.0009765625);

float CLength(vec3 c1){

	return length(c1);

}

float Cdistance(vec3 c1, vec3 c2){

	return distance(c1,c2);

}
 
vec3 ColMin(vec3 a, vec3 b){

	float dist = step(0.1,sign(CLength(a) - CLength(b)));

	return mix(a,b,dist);

}

vec3 ColMax(vec3 a, vec3 b){

	float dist = step(0.1,sign(CLength(a) - CLength(b)));

	return mix(b,a,dist);

}
 
//	4xGLSoft lite code ( some modification... )

vec3 lite4xGLSoft(vec2 texcoord) {

	vec3 c11 = texture2D(OGL2Texture, texcoord        ).rgb; 
	vec3 c00 = texture2D(OGL2Texture, texcoord + xy.zw).rgb; 
	vec3 c20 = texture2D(OGL2Texture, texcoord + xy.xw).rgb; 
	vec3 c22 = texture2D(OGL2Texture, texcoord + xy.xy).rgb; 
	vec3 c02 = texture2D(OGL2Texture, texcoord + xy.zy).rgb; 
	vec3 s00 = texture2D(OGL2Texture, texcoord + yx.zw).rgb; 
	vec3 s20 = texture2D(OGL2Texture, texcoord + yx.xw).rgb; 
	vec3 s22 = texture2D(OGL2Texture, texcoord + yx.xy).rgb; 
	vec3 s02 = texture2D(OGL2Texture, texcoord + yx.zy).rgb; 

	vec4 d1=vec4(
	Cdistance(c00,c22),
	Cdistance(c20,c02),
	Cdistance(s00,s22),
	Cdistance(s02,s20))+0.001;

	vec3 t2=(d1.x*(c20+c02)+d1.y*(c00+c22))/(2.0*(d1.x+d1.y));
	
	return .25*(c11+t2+(d1.w*(s00+s22)+d1.z*(s02+s20))/(d1.z+d1.w));

}

//      de Blur code ( some modification... )

vec3 deBlur(vec2 texCoord) {

	vec3 C0 = lite4xGLSoft(texCoord-g1); 
	vec3 C1 = lite4xGLSoft(texCoord-dy);
	vec3 C2 = lite4xGLSoft(texCoord-g2);
	vec3 C3 = lite4xGLSoft(texCoord-dx);
	vec3 C4 = lite4xGLSoft(texCoord   );
	vec3 C5 = lite4xGLSoft(texCoord+dx);
	vec3 C6 = lite4xGLSoft(texCoord+g2);
	vec3 C7 = lite4xGLSoft(texCoord+dy);
	vec3 C8 = lite4xGLSoft(texCoord+g1);

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

	float dif1 = Cdistance(C4,mn1) + 0.001;
	float dif2 = Cdistance(C4,mx1) + 0.001;
 
	float filterparam = 6.0; // de Blur control

	dif1=pow(dif1,filterparam);
	dif2=pow(dif2,filterparam);
	float dif3=dif1+dif2; 
 
	return (dif1*mx1 + dif2*mn1)/dif3;

}

void main()
{

	vec3 c11 = deBlur( gl_TexCoord[0].xy );

	gl_FragColor.rgb = c11;

}
