//
// Luminance downsampling - from linear (HDR) to log-average luminance
// Used in the first stage of luminance detection:
// 1. Downsample anti-aliased scene FBO (for example, from 1280x800 -> 256x256), calculating log luminance
// 2. Downsample 256x256 luminance FBO to 1x1 FBO
// 3. Use that pixel and apply exp() to get scene luminance
//
// RvG, 26-8-10
//

struct v2f
{
  float2 tc0        : TEXCOORD0;
};

struct FragOut
{
	float4 col : COLOR;
};

FragOut main(
  v2f IN,
  uniform sampler2D tex : TEXUNIT0
)
{
  FragOut OUT;
  //const float3 rgb2luminance=float3(0.2125,0.7154,0.0721);   // Weighing RGB
  const float3 rgb2luminance=float3(0.28,0.59,0.13);   // Weighing RGB
  const float  delta=0.0001;
  
  // Take 1 sample - although we miss out some data, we assume the average won't
  // change that much
  OUT.col=log(dot(tex2D(tex,IN.tc0),rgb2luminance)+delta);
  
  return OUT;
}
