/* COMPATIBILITY 
   - HLSL compilers
   - Cg   compilers
*/

void main_vertex
(
	half4 position	: POSITION,
	half2 texCoord : TEXCOORD0,

	uniform float4x4 modelViewProj,

	out half4 oPosition : POSITION,
	out half2 otexCoord : TEXCOORD
)
{
	oPosition = mul(modelViewProj, position);
	otexCoord = texCoord;
}

half Luminance = 1.00f;
static const half fMiddleGray = 0.18f;
static const half fWhiteCutoff = 0.8f;

#define NUM 13

half2 PixelOffsets[NUM] =
{
   { -0.006, -0.006 },
   { -0.005, -0.005 },
   { -0.004, -0.004 },
   { -0.003, -0.003 },
   { -0.002, -0.002 },
   { -0.001, -0.001 },
   {  0.000,  0.000 },
   {  0.001,  0.001 },
   {  0.002,  0.002 },	
   {  0.003,  0.003 },
   {  0.004,  0.004 },
   {  0.005,  0.005 },
   {  0.006,  0.006 },
};

static const half BlurWeights[NUM] =
{
   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,
};

struct input
{
  float2 video_size;
  float2 texture_size;
  float2 output_size;
  float frame_count;
  float frame_direction;
  float frame_rotation;
};

struct output 
{
  half4 color    : COLOR;
};


output main_fragment( half2 Tex : TEXCOORD0, uniform input IN, uniform sampler2D s0 : TEXUNIT0 )
{
   half2 BloomSettings;
   BloomSettings.x = 1;
   BloomSettings.y = 0.3;

   half3 pixel;
   half3 Color = 0;

   for(int i = 0; i < NUM; i++)
   {
      pixel = tex2D(s0, Tex + 0.5 * PixelOffsets[i] * 5.0f)+BloomSettings.y;

      pixel *= fMiddleGray / (Luminance + 0.001f);
      pixel *= (1.0f + (pixel / (fWhiteCutoff * fWhiteCutoff)));
      pixel -= 5.0f;

      pixel = max(pixel,0.0f);
      pixel /= (10.0f + pixel);

      Color += pixel * BlurWeights[i];
   }

   Color *= BloomSettings.x;

   output OUT;
   OUT.color = half4(Color,1.0) + tex2D(s0,Tex);
   return OUT;
}

