#define NUM_SAMPLES 64

#include "SpriteBatchVertexShader.fx"

sampler2D	gTextureMask	: register( s0 );
float2		gScreenLightPos;
float		gDensity;
float		gDecay;
float		gWeight;
float		gExposure;

struct VS_OUT
{
    float4 Position : POSITION0;
	float2 TexCoord	: TEXCOORD0;
};
// ---------------------------------------------------------
float4 PixelShaderFunction( VS_OUT IN,
	uniform sampler2D TextureMask,
	uniform float2 ScreenLightPos,
	uniform float Density,
	uniform float Decay,
	uniform float Weight,
	uniform float Exposure ) : COLOR0
{
	float2 TexCoord = IN.TexCoord;
	float2 DeltaTexCoord = ( TexCoord.xy - ScreenLightPos.xy );
	float Len = length( DeltaTexCoord );
	DeltaTexCoord *= 1.0 / NUM_SAMPLES * Density;
	float3 Color = tex2D( TextureMask, TexCoord );
	float IlluminationDecay = 1.0;
	for( int i = 0; i < NUM_SAMPLES; ++i )
	{
		TexCoord -= DeltaTexCoord;
		float3 Sample = tex2D( TextureMask, TexCoord );
		Sample *= IlluminationDecay * Weight;
		Color += Sample;
		IlluminationDecay *= Decay;	
	}
	return float4( Color * Exposure, 1.0 );
}
// ---------------------------------------------------------
technique Technique1
{
    pass p0
    {
		VertexShader = compile vs_3_0 SpriteVertexShader( );
        PixelShader = compile ps_3_0 PixelShaderFunction( 
			gTextureMask,
			gScreenLightPos,
			gDensity,
			gDecay,
			gWeight,
			gExposure );
    }
}
