//
// Blur horizontally
// Input: HDR image
// Output: horizontally blurred
//
// Path: HDR-> FBO1 -> FBO2, mix HDR+FBO2 -> screen
// FBO1 = horizontally blurred image, FBO2=also vertically blurred
//
// RvG, 10-3-09
//

#include "common_fs.cg"

struct VertOut
{
	float4 pos : POSITION;
	float4 col : COLOR0;
	float2 tc0 : TEXCOORD0;
};

struct FragOut
{
	float4 col : COLOR;
};

float4 SuppressLDR(float4 c)
// Skip all dark pixels (LDR ones)
// Returns additive results
{
  const float low=0.75;
  if(c.r>low || c.g>low || c.b>low)
  {
    // Only return result above the threshold
    return max(c-low,0);
  } else
  {
    // Skip
    return float4(0,0,0,0);
  }
}

float4 SuppressLDR_test(float4 c)
// Skip all dark pixels, leaves bright pixels
{
  return max((c-0.6)*1.5,0);
  //return max((c-0.65)*2.86,0);
  //return max((c-0.8)*5.0,0);
  //return max((c-0.5)*2.0,0);
}

FragOut main(
  VertOut vin,
	uniform sampler2D sceneTex : TEXUNIT0
)
{
  FragOut fout;
  float2 BlurOffset=float2(1.0/1024.0, 0);
  float PixelWeight[8]={ 0.2537, 0.2185, 0.0821, 0.0461, 0.0262, 0.0162, 0.0102, 0.0052 };    // Sum=0.6582?!

  float4 color=0; //tex2D(tex0,vin.tc0);

  // Sample pixels on either side
  for(int i=0;i<8;++i)
  {
    color+=SuppressLDR(tex2D(sceneTex,vin.tc0+(BlurOffset*i)))*PixelWeight[i];
    //color+=tex2D(tex0, vin.tc0+(BlurOffset*i))*PixelWeight[i];
    color+=SuppressLDR(tex2D(sceneTex,vin.tc0-(BlurOffset*i)))*PixelWeight[i];
    //color+=tex2D(tex0, vin.tc0-(BlurOffset*i))*PixelWeight[i];
  }

  // Take out some colorizing
  //color=pow(color,4);
  float4 bloomColor=Desaturate(color,1.0); //SuppressLDR(color);

  //float mix=0.5;
  //fout.col=mix*color+(1.0-mix)*bloomColor;
//bloomColor=float4(0,1,0,1);
  fout.col=bloomColor;

  return fout;
} 
