#pragma parameter EQ_THRESH2 "Eq Limit Vertical" 0.01 0.0 1.0 0.01
#pragma parameter DIFF_THRESH2 "Diff Limit Vertical" 0.06 0.0 1.0 0.01
#ifdef PARAMETER_UNIFORM
uniform float EQ_THRESH2;
uniform float DIFF_THRESH2;
#else
#define EQ_THRESH2 0.01
#define DIFF_THRESH2 0.06
#endif
// END PARAMETERS //

/* COMPATIBILITY 
   - HLSL compilers
   - Cg   compilers
*/


/*
   Hyllian's Deposterize Shader - Pass1
   
   Copyright (C) 2011/2016 Hyllian/Jararaca - sergiogdb@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.

*/

#define EQ   EQ_THRESH2
#define DF  DIFF_THRESH2

float3 df3(float3 c1, float3 c2)
{
      return abs(c1 - c2);
}

bool3 eq3(float3 A, float3 B)
{
	return (df3(A, B) <= float3(EQ));
}


struct input
{
	float2 video_size;
	float2 texture_size;
	float2 output_size;
};


struct out_vertex {
	float4 position : POSITION;
	float4 color    : COLOR;
	float2 texCoord1: TEXCOORD0;
	float4 t1       : TEXCOORD1;
};

/*    VERTEX_SHADER    */
out_vertex main_vertex
(
	float4 position	: POSITION,
	float4 color	: COLOR,
	float2 texCoord1 : TEXCOORD0,

   	uniform float4x4 modelViewProj,
	uniform input IN
)
{
	out_vertex OUT;

	OUT.position = mul(modelViewProj, position);
	OUT.color = color;

	float2 ps = float2(1.0/IN.texture_size.x, 1.0/IN.texture_size.y);
	float dx = ps.x;
	float dy = ps.y;

	// This line fix a bug in ATI cards.
	float2 texCoord = texCoord1 + float2(0.0000001, 0.0000001);

	OUT.texCoord1 = texCoord;
	OUT.t1 = texCoord.xyyy + float4(   0, -dy, 0,  dy); //  B  E  H

	return OUT;
}


/*    FRAGMENT SHADER    */
float4 main_fragment(in out_vertex VAR, uniform sampler2D decal : TEXUNIT0, uniform input IN) : COLOR
{
	float3 res;

	float3 B  = tex2D(decal, VAR.t1.xy).rgb;
	float3 E  = tex2D(decal, VAR.t1.xz).rgb;
	float3 H  = tex2D(decal, VAR.t1.xw).rgb;

	res = ((!eq3(B, H)) && ((df3(E, H) <= float3(DF, DF, DF)) && (eq3(B, E)) || (df3(B, E) <= float3(DF, DF, DF)) && (eq3(E, H)))) ? 0.5*(B+H) : E;

	return float4(res, 1.0);
}


