/* COMPATIBILITY 
   - HLSL compilers
   - Cg   compilers
*/

/* 4xSoft Smart deBlur shader
   
   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.

*/


#pragma parameter RESOLUTION_X "4xSoft Input Resolution X" 0.0 0.0 1920.0 1.0
#pragma parameter RESOLUTION_Y "4xSoft Input Resolution Y" 0.0 0.0 1920.0 1.0
#pragma parameter CONTRAST     "4xSoft Contrast"           3.0 0.0 10.0 0.1

#define RESOLUTION_X_DEF IN.texture_size.x
#define RESOLUTION_Y_DEF IN.texture_size.y
#ifdef PARAMETER_UNIFORM
uniform float RESOLUTION_X;
uniform float RESOLUTION_Y;
uniform float CONTRAST;
#else
#define RESOLUTION_X RESOLUTION_X_DEF
#define RESOLUTION_Y RESOLUTION_Y_DEF
#define CONTRAST 3.0
#endif

const static float3 dt = float3(1.0, 1.0, 1.0);
const static float3 dtt = float3(0.001,0.001,0.001);


struct input
{
	float2 video_size;
	float2 texture_size;
	float2 output_size;
	float frame_count;
	float frame_direction;
	float frame_rotation;
};


struct out_vertex {
	float4 position : POSITION;
	float2 texCoord : TEXCOORD0;
	float4 t1 : TEXCOORD1;
	float4 t2 : TEXCOORD2;
	float4 t3 : TEXCOORD3;
	float4 t4 : TEXCOORD4;
	float4 t5 : TEXCOORD5;
	float4 t6 : TEXCOORD6;
};

/*    VERTEX_SHADER    */
out_vertex main_vertex
(
	float4 position	: POSITION,
	float2 texCoord  : TEXCOORD0,

   	uniform float4x4 modelViewProj,
	uniform input IN
)
{
   /* messy I know but we need to make it possible to have it default to input resolution x/y in case RESOLUTION_X is 0.0 */
	float2 ps = float2(1.0/IN.texture_size.x, 1.0/IN.texture_size.y); 

	float dx = ps.x*1.0;
	float dy = ps.y*1.0;
	float sx = ps.x*0.5;
	float sy = ps.y*0.5;


	out_vertex OUT = { 
		mul(modelViewProj, position),
		texCoord,
		float4(texCoord,texCoord) + float4(-dx, -dy, dx, -dy), // outer diag. texels
		float4(texCoord,texCoord) + float4(dx, dy, -dx, dy),
		float4(texCoord,texCoord) + float4(-sx, -sy, sx, -sy), // inner diag. texels
		float4(texCoord,texCoord) + float4(sx, sy, -sx, sy),
		float4(texCoord,texCoord) + float4(-dx, 0, dx, 0), // inner hor/vert texels
		float4(texCoord,texCoord) + float4(0, -dy, 0, dy)
	};


	return OUT;
}


float4 main_fragment(in out_vertex VAR, uniform sampler2D decal : TEXUNIT0, uniform input IN) : COLOR
{

  float3 c11 = tex2D(decal, VAR.texCoord).xyz;
  float3 c00 = tex2D(decal, VAR.t1.xy).xyz;
  float3 c20 = tex2D(decal, VAR.t1.zw).xyz;
  float3 c22 = tex2D(decal, VAR.t2.xy).xyz;
  float3 c02 = tex2D(decal, VAR.t2.zw).xyz;
  float3 s00 = tex2D(decal, VAR.t3.xy).xyz;
  float3 s20 = tex2D(decal, VAR.t3.zw).xyz;
  float3 s22 = tex2D(decal, VAR.t4.xy).xyz;
  float3 s02 = tex2D(decal, VAR.t4.zw).xyz;
  float3 c01 = tex2D(decal, VAR.t5.xy).xyz;
  float3 c21 = tex2D(decal, VAR.t5.zw).xyz;
  float3 c10 = tex2D(decal, VAR.t6.xy).xyz;
  float3 c12 = tex2D(decal, VAR.t6.zw).xyz;

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

  float3 mn1 = min (min (c00,c01),c02);
  float3 mn2 = min (min (c10,c11),c12);
  float3 mn3 = min (min (c20,c21),c22);
  float3 mx1 = max (max (c00,c01),c02);
  float3 mx2 = max (max (c10,c11),c12);
  float3 mx3 = max (max (c20,c21),c22);

  mn1 = min(min(mn1,mn2),mn3);
  mx1 = max(max(mx1,mx2),mx3);

  float3 t1=(hl*(c10+c12)+vl*(c01+c21)+(hl+vl)*c11)/(3.0*(hl+vl));
  float3 t2=(d1*(c20+c02)+d2*(c00+c22)+(d1+d2)*c11)/(3.0*(d1+d2));

  c11 = 0.25*(t1+t2+(m2*(s00+s22)+m1*(s02+s20))/(m1+m2));

  float3 dif1 = abs(c11-mn1) + dtt;
  float3 dif2 = abs(c11-mx1) + dtt;

//	float filterparam = 2.0;

  float dif = max(length(dif1),length(dif2));
  float filterparam = clamp(2.25*dif,1.0,2.0);
  
  dif1=float3(pow(dif1.x,filterparam),pow(dif1.y,filterparam),pow(dif1.z,filterparam));
  dif2=float3(pow(dif2.x,filterparam),pow(dif2.y,filterparam),pow(dif2.z,filterparam));

  c11 = float3((dif1.x*mx1.x + dif2.x*mn1.x)/(dif1.x + dif2.x),
               (dif1.y*mx1.y + dif2.y*mn1.y)/(dif1.y + dif2.y),
			   (dif1.z*mx1.z + dif2.z*mn1.z)/(dif1.z + dif2.z));

  return float4(c11,1.0);
}
