//
// Utility functions
//

float3 UnpackNormal(float3 c)
// Leaves .a alone (to avoid negative alpha and such)
// Used for bumpmapping shaders mainly, where a normal is stored in an RGB map
{
  // Only do XYZ, leave W (alpha?) alone
  c=2*c-1;
  return c;
}

float3 ColorGrade(float3 color,sampler3D tex)
// Stereo's version
// Colorgrade using the texture as a basis
// See http://www.racer.nl/tutorial/colorgrading.htm
// Bugs: G and B are mixed, B is negated; probably all due to DirectX type axes directions.
// Perhaps fixed in the future by preprocessing the 3D map to flip some axes.
{
  //return color;
  //
  // Avoid sampling in borders - should offset & scale
  // See also http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter24.html
  //
  //const float limit=1.0/16.0;
  //color=clamp(color,limit,1-limit);
  const float lutSize=16.0;           // Size of each 2D texture in the 3D map, corrected for sun disc artifacts

  half3 scale =(lutSize-1.0)/lutSize;
  half3 offset=1.0/(2.0*lutSize);
  //half3 scale =(lutSize-1.0)/lutSize;
  //half3 offset=(1-scale)*0.5;

  color.g=1-color.g;

  // Pass through color correction map (notice the swizzle of B and G!)
  color.rgb=tex3D(tex,scale*color.rbg+offset);

  return color;
}

/*
float3 ColorGrade1(float3 color,sampler3D tex)
// RvG version v0.8.40
// Colorgrade using the texture as a basis
// See http://www.racer.nl/tutorial/colorgrading.htm
// Bugs: G and B are mixed, B is negated; probably all due to DirectX type axes directions.
// Perhaps fixed in the future by preprocessing the 3D map to flip some axes.
{
  // Avoid sampling in borders - should offset & scale
  // See also http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter24.html
  //const float limit=1.0/16.0;
  //color=clamp(color,limit,1-limit);
  const float lutSize=12.0;           // Size of each 2D texture in the 3D map, corrected for sun disc artifacts

  half3 scale =(lutSize-2.0)/lutSize;
  half3 offset=(1-scale)*0.5;
  
  color.b=1-color.b;
  //color.g=1-color.g;
  
  // Pass through color correction map
  color.rbg=tex3D(tex,scale*color+offset);
  
  return color;
}

float3 ColorGrade2(float3 color,sampler3D tex)
// RvG version; v0.8.44
// Colorgrade using the texture as a basis
// See http://www.racer.nl/tutorial/colorgrading.htm
// Bugs: G and B are mixed, B is negated; probably all due to DirectX type axes directions.
// Perhaps fixed in the future by preprocessing the 3D map to flip some axes.
{
  // Avoid sampling in borders - should offset & scale
  // See also http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter24.html
  //const float limit=1.0/16.0;
  //color=clamp(color,limit,1-limit);
  const float lutSize=16.0;           // Size of each 2D texture in the 3D map, corrected for sun disc artifacts
  
  //half3 scale =(lutSize-2.0)/lutSize;
  //half3 offset=(1-scale)*0.5;
  half3 offset= 1.0 / (2.0 * lutSize);  
  half3 scale = (lutSize - 1.0) / lutSize;
  color.b=1-color.b;
  //color.g=1-color.g;
  
  // Pass through color correction map
  color.rgb=tex3D(tex,scale*color.rgb+offset);
  
  return color;
}
*/
