
#include "common_ps_fxc.h"

// filter threshold
float Threshold		: register( c2 );

// screen sampler
sampler ScreenSampler	: register( s0 );

/*------------------------------------
	ShaderInput
------------------------------------*/
struct ShaderInput
{
	float2 TextureCoord	: TEXCOORD0;
};

/*------------------------------------
	main()
------------------------------------*/
float4 main( ShaderInput input ) : COLOR
{
	
	float4 color = tex2D( ScreenSampler, input.TextureCoord );
	
	const int num = 9;
	const float2 c[ num ] = {
		float2( -0.0078125, 0.0078125 ), 
		float2( 0.00, 0.0078125 ),
		float2( 0.0078125, 0.0078125 ),
		float2( -0.0078125, 0.00 ),
		float2( 0.0, 0.0),
		float2( 0.0078125, 0.007 ),
		float2( -0.0078125, -0.0078125 ),
		float2( 0.00, -0.0078125 ),
		float2( 0.0078125, -0.0078125 ),
	};
	
	int i;
	float3 col[ num ];
	for( i = 0; i < num; i++ )
		col[ i ] = tex2D( ScreenSampler, input.TextureCoord + 0.2 * c[ i ] );
		
	// calculate luminance
	float3 rgb2lum = float3( 0.3f, 0.59f, 0.11f );
	float lum[ num ];
	for( i = 0; i < num; i++ )
		lum[ i ] = dot( col[ i ].xyz, rgb2lum );
	
	// sobel filter
	float x = lum[ 2 ] + lum[ 8 ] + 2 * lum[ 5 ] - lum[ 0 ] - 2 * lum[ 3 ] - lum[ 6 ];
	float y = lum[ 6 ] + 2 * lum[ 7 ] + lum[ 8 ] - lum[ 0 ] - 2 * lum[ 1 ] - lum[ 2 ];
	float edge = ( ( x * x + y * y ) < Threshold ) ? 1.0f : 0.0f;
	
	color *= edge;

	return color;
	
}
