/*
/   "Conditional Blending of Dither" Shader v1.0 - Pass 1
/   Copyright (c) 2013, Alexander Kulagin <coastkid3d@gmail.com>
/   All Rights reserved.
/ 
/   Redistribution and use in source and binary forms, with or without
/   modification, are permitted provided that the following conditions are met:
/ 
/     * Redistributions of source code must retain the above copyright notice,
/       this list of conditions and the following disclaimer.
/ 
/     * Redistributions in binary form must reproduce the above copyright
/       notice, this list of conditions and the following disclaimer in the
/       documentation and/or other materials provided with the distribution.
/ 
/   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
/   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
/   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
/   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
/   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
/   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
/   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
/   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
/   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
/   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
/   POSSIBILITY OF SUCH DAMAGE.
*/

/* COMPATIBILITY 
   - HLSL compilers
   - Cg   compilers
   - FX11 compilers
*/

//////////////////
// VERTEX_SHADER

#include "../../../compat_includes.inc"
uniform COMPAT_Texture2D(decal) : TEXUNIT0;
uniform float4x4 modelViewProj;

struct out_vertex
{
	float4 position : COMPAT_POS;
	float2 texCoord : TEXCOORD0;
};

out_vertex main_vertex(COMPAT_IN_VERTEX)
{
#ifdef HLSL_4
	float4 position = VIN.position;
	float2 texCoord = VIN.texCoord;
#endif
	out_vertex OUT;
	OUT.position = mul(modelViewProj, position);
	OUT.texCoord = texCoord;
	
	return OUT;
}

//////////////////
// FRAGMENT SHADER

float4 cbod_v1_pass1(float2 texture_size, float2 texCoord, COMPAT_Texture2D(tex0))
{ 
  float2 uv = texCoord - (1.0f / texture_size) * 0.25f;
  float2 uv_shift = (1.0f / texture_size);
	float3 src = COMPAT_SamplePoint(tex0, uv).rgb;
 
  // Searching for the Vertical Dithering Zones
	float3 dither_v_zone = COMPAT_SamplePoint(tex0, uv + float2(uv_shift.x, 0)).rgb == COMPAT_SamplePoint(tex0, uv - float2(uv_shift.x, 0)).rgb;
  dither_v_zone = smoothstep(0.2f, 1.0f, dot(dither_v_zone, 0.33333f));
  
  // Searching for High Contrast "Safe" Zones 
  float3 safe_zone = abs(dot(COMPAT_SamplePoint(tex0, uv) - COMPAT_SamplePoint(tex0, uv - float2(uv_shift.x, 0)),0.3333f));
  safe_zone = safe_zone < 0.45f;
 
  // Horizontal Bluring by 1 pixel
  float3 blur_h = (COMPAT_SamplePoint(tex0, uv).rgb + COMPAT_SamplePoint(tex0, uv - float2(uv_shift.x, 0)).rgb) * 0.5f;
  
  // Final Blend between Source and Blur using Dithering Zone and Safe Zone
  float3 finalcolor = lerp(src, blur_h, dither_v_zone * safe_zone);
  
  return float4(finalcolor, 1.0f);
}

float4 main_fragment(COMPAT_IN_FRAGMENT) : COMPAT_Output
{
	return cbod_v1_pass1(COMPAT_texture_size, VOUT.texCoord, decal);
}
COMPAT_END