//
// Hold only bright pixels (brightness pass)
// Input: HDR image
// Output: bright pixels only
//
// RvG, 21-4-09
//

#include "common_fs.cg"

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

struct FragOut
{
	float4 col : COLOR;
};

half4 SuppressLDR_v1(half4 c)
// Skip all dark pixels, leaves bright pixels
{
  //return max((c-2.0)*1.5,0);
  return max(c/100-0.4,0);
  //return max((c-0.5)*2.5,0);
  //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);
}

float4 SuppressLDR_v2(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_v3(float4 color)
{
  const float BRIGHT_PASS_THRESHOLD = 0.8;
  const float BRIGHT_PASS_OFFSET = 0.01;
	
  color=max(color-BRIGHT_PASS_THRESHOLD, 0.0);

  return color/(color + BRIGHT_PASS_OFFSET);
}
  
FragOut main(
  VertOut vin,
	uniform sampler2D sceneTex : TEXUNIT0
)
{
  
  FragOut fout;
  half4 color=SuppressLDR_v1(tex2D(sceneTex,vin.tc0));
  /*
  int ctest;
  float tx=1/700;
  float ty=1/480;
  float2 offset[8]={float2(tx,ty),
					float2(0,ty),
					float2(tx,0),
					float2(-tx,-ty),
					float2(0,-ty),
					float2(-tx,0),
					float2(-tx,ty),
					float2(tx,-ty)
					};
					
  for (int i=0;i<8;i++)
  {
	half4 coloraro=SuppressLDR_v1(tex2D(sceneTex,vin.tc0+offset[i]));
	if (coloraro.r==0&&coloraro.g==0&&coloraro.b==0)
	{
		ctest++;
	}
  }
			
  
  if (ctest==8)
  {
	color=0;
  }
  */
  
  //color.a=0;

  // Take out some colorizing
  fout.col=Desaturate(color,0.2);

  return fout;
} 
