Texture2D colorIn : register( t0 );
SamplerState samPoint : register( s0 );
SamplerState samLinear : register( s1 );

static const int NUM_SAMPLES = 20;
static const float Density = 1.36f; //1.36 fuer 20
static const float Decay = 0.90f;
static const float Exposure = 0.1f;

cbuffer cbChangesEveryLight : register( b0 )
{
	float4 lightPos;
}

//--------------------------------------------------------------------------------------
struct VS_INPUT
{
    float2 Pos : POSITION;
    float2 Tex : TEXCOORD0;
};

struct PS_INPUT
{
    float4 Pos : SV_POSITION;
    float2 Tex : TEXCOORD0;
};

//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
PS_INPUT VS(VS_INPUT input)
{
    PS_INPUT output = (PS_INPUT)0;
    output.Pos = float4(input.Pos, 0.0f, 1.0f);
    output.Tex = input.Tex;
    
    return output;
}

//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 PS(PS_INPUT input) : SV_TARGET
{
	float2 texCoord = input.Tex;
	float2 deltaTexCoord = texCoord;
	deltaTexCoord.x -= (lightPos.x + 1.0f)/ 2.0f;
	deltaTexCoord.y -= (lightPos.y - 1.0f)/-2.0f;
	
	deltaTexCoord /= (float2)(NUM_SAMPLES * Density); //NUM_SAMPLES / DENSITY
	
	half3 color = colorIn.Sample(samLinear, texCoord);
	
	half illuminationDecay = 1.0f;
	
	for(int i = 0; i < NUM_SAMPLES; i++)
	{
		texCoord -= deltaTexCoord;
		
		half3 sample = colorIn.Sample(samLinear, texCoord);
		sample *= illuminationDecay * 1.0f; // WEIGHT
		
		color += sample;
		illuminationDecay *= Decay; // DECAY
		
	}
	
	return float4(color * Exposure, 1.0f);
}

float4 PS_REDUCE(PS_INPUT input) : SV_TARGET
{
	return colorIn.Sample(samLinear, input.Tex);
}