texture g_texture;

sampler2D g_samSrcColor =
sampler_state
{
    Texture = <g_texture>;
};


float BulletTime=1.0f;
float ScreenMoveX=0.0f;
float ScreenMoveY=0.0f;

float bloom_threshold = 0.5f;
float bloom_multiply = 5.0f;
float bloom_blursize = 1.0;
float bloom_colour = 0.5;

float PixelSizeX = 0.001f;
float PixelSizeY = 0.001f;


// all these are stolen from the DirectX SDK - sorry Bill, hope you don't mind!
static const int g_cKernelSize = 13;


static const float BlurWeights[g_cKernelSize] = 
{
    0.002216,
    0.008764,
    0.026995,
    0.064759,
    0.120985,
    0.176033,
    0.199471,
    0.176033,
    0.120985,
    0.064759,
    0.026995,
    0.008764,
    0.002216,
};


float2 DownCoords[16] =
{
    { 1.5,  -1.5 },
    { 1.5,  -0.5 },
    { 1.5,   0.5 },
    { 1.5,   1.5 },

    { 0.5,  -1.5 },
    { 0.5,  -0.5 },
    { 0.5,   0.5 },
    { 0.5,   1.5 },

    {-0.5,  -1.5 },
    {-0.5,  -0.5 },
    {-0.5,   0.5 },
    {-0.5,   1.5 },

    {-1.5,  -1.5 },
    {-1.5,  -0.5 },
    {-1.5,   0.5 },
    {-1.5,   1.5 },
};

float BloomScale = 1.5f;


float4 DownSample( float2 Tex : TEXCOORD0 ) : COLOR0
{
    float4 Color=0;
       
    for (int i = 0; i < 16; i++)
    {
        Color += tex2D( g_samSrcColor, Tex + float2(DownCoords[i].x*PixelSizeX, DownCoords[i].y*PixelSizeY) );
    }
    
    
    Color = Color / 16.0f;
    
    
    Color = Color - bloom_threshold;
    Color = Color * bloom_multiply;
    
    Color.r = (((Color.r+Color.g+Color.b)/3.0f)*(1.0f-bloom_colour)) + (Color.r*bloom_colour);
    Color.g = (((Color.r+Color.g+Color.b)/3.0f)*(1.0f-bloom_colour)) + (Color.g*bloom_colour);
    Color.b = (((Color.r+Color.g+Color.b)/3.0f)*(1.0f-bloom_colour)) + (Color.b*bloom_colour);

   
    Color.a = 1.0f;
          
    return Color;
}




float4 HBlur( float2 Tex : TEXCOORD0 ) : COLOR0
{
    float4 Color=0;
    
    for (int i = -6; i <= 6; i++)
    {    
        Color += tex2D( g_samSrcColor, Tex + (float2(((float)i)*bloom_blursize*PixelSizeX,0.0f))) * BlurWeights[i+6];
    }
    
	Color.a = 1.0f;
    return Color;
}


float4 VBlur( float2 Tex : TEXCOORD0 ) : COLOR0
{
    float4 Color=0;
    
    for (int i = -6; i <= 6; i++)
    {    
        Color += tex2D( g_samSrcColor, Tex + (float2(0.0f,((float)i)*bloom_blursize*PixelSizeY))) * BlurWeights[i+6];
    }

	Color.a = 1.0f;
    return Color;
}

technique One
{
    pass p0
    {
        VertexShader = null;
        PixelShader = compile ps_2_0 DownSample();
    }
    
    pass p1
    {
        VertexShader = null;
        PixelShader = compile ps_2_0 HBlur();
    }
    pass p2
    {
        VertexShader = null;
        PixelShader = compile ps_2_0 VBlur();
    }
    
}
