////////////////////////////////////////////////////////////////////////////////////////////////////
// Includes
////////////////////////////////////////////////////////////////////////////////////////////////////

#include "../../../types.txt"

#define MIN_SAMPLE 4
#define MAX_SAMPLE 64

////////////////////////////////////////////////////////////////////////////////////////////////////
// Structures
////////////////////////////////////////////////////////////////////////////////////////////////////

struct PS_INPUT
{
#if MIX_SM_HIGH
	float4 pos  : SV_POSITION;
#endif //MIX_SM_HIGH
	float2 tex : TEXCOORD0;
};

////////////////////////////////////////////////////////////////////////////////////////////////////
// Grobal values
////////////////////////////////////////////////////////////////////////////////////////////////////

#if MIX_SM_HIGH

	cbuffer cbInput : register( b0 )
	{
		float4 g_NumSample;
		float4 g_Inc;
		float4 g_Step;
	}

	Texture2D g_ColorTexture    : register( t0 );
	SamplerState g_ColorSampler : register( s0 );
	
#else //MIX_SM_HIGH
	
	float4 g_NumSample : register( c0 );
	float4 g_Inc       : register( c1 );
	float4 g_Step      : register( c2 );

	sampler g_ColorSampler : register( s0 );

#endif //MIX_SM_HIGH

////////////////////////////////////////////////////////////////////////////////////////////////////
// Macros
////////////////////////////////////////////////////////////////////////////////////////////////////

#if MIX_SM_HIGH
	#define TEX_COLOR( uv ) g_ColorTexture.Sample( g_ColorSampler, uv )
#else //MIX_SM_HIGH
	#define TEX_COLOR( uv ) tex2D( g_ColorSampler, uv )
#endif //MIX_SM_HIGH

// radius / 2.0f
#define G_NUN_SAMPLE_F g_NumSample.x

// direction = float2( 1.0, 0.0 ) or float2( 0.0, 1.0 )
// texelSize = float2( 1.0 / textureWidth, 1.0 / textureHeight )
// direction * texelSize
#define G_STEP g_Step

// TWO_PI=6.2831853071795
// SIGMA=radius / 8.0f
// SIGMA2=SIGMA*SIGMA
// x=1.0 / ( sqrt( TWO_PI ) * SIGMA ) y=exp( -0.5 / SIGMA2 ) z=y*y
#define G_INC g_Inc

////////////////////////////////////////////////////////////////////////////////////////////////////
// Main functions
////////////////////////////////////////////////////////////////////////////////////////////////////

float4 main( PS_INPUT input ) : MSV_TARGET0
{
	int numSample = clamp( ( int )G_NUN_SAMPLE_F, MIN_SAMPLE, MAX_SAMPLE );

	float2 offset = G_STEP.xy;
	float3 inc = G_INC.xyz;

	float4 output = TEX_COLOR( input.tex ) * inc.x;

	[unroll(MAX_SAMPLE)]
	for( int i = 1; i < numSample; i++ )
	{
		inc.xy *= inc.yz;

		output += TEX_COLOR( input.tex - offset) * inc.x;
		output += TEX_COLOR( input.tex + offset) * inc.x;

		offset += G_STEP.xy;
	}

	return output;
}
