


// Specifics to this shader
uniform int nTextureSize;
uniform int anTransparantTable[16];
uniform int nTableSize;
uniform bool isInterpolate;

uniform float rOffset;
uniform float rScale;




/////////////////////////////////////////////////////////////////////////////
// IsInvisibleRangeScan
//

bool
IsInvisibleRangeScan(float rMin, float rMax)
{

  float
    rTexturePosMin = clamp(rMin * float(nTextureSize), .5, float(nTextureSize) - .5),
    rTexturePosMax = clamp(rMax * float(nTextureSize), .5, float(nTextureSize) - .5);

  int
    nTexelMin,
    nTexelMax;

  if (isInterpolate)
  {
    nTexelMin =  int (floor (rTexturePosMin - .5) );
    nTexelMax =  int (ceil  (rTexturePosMax + .5) );
  }
  else
  {
    nTexelMin = int (floor (rTexturePosMin) );
    nTexelMax = int (ceil  (rTexturePosMax) );
  }

  int
    iElement = 0;

  for (iElement = 0; iElement < nTableSize; iElement++)
  {
    int
      iFlip = anTransparantTable[iElement];

    if (iFlip > nTexelMin)
    {
      break;
    }
  }

  bool
    isOnMarker = (iElement % 2) == 1;

  if (iElement == nTableSize)
  {
    // Low and high above all value points.
    return isOnMarker;
  }

  if (isOnMarker)
  {
    // Low is on OFF interval
    // If High is on same interval, it is also OFF, and we can space leap. Else not.
    int
      iFlip = anTransparantTable[iElement];

    bool
      isHighOnSameInterval = iFlip >= nTexelMax;

    return isHighOnSameInterval;
  }
  else
  {
    // Low is on ON interval. Doesn't matter with High.
    return false;
  }
}

/////////////////////////////////////////////////////////////////////////////
// IsInvisible
//

bool
IsInvisible(ivec3 iMin, ivec3 iMax)
{
  if (nTableSize == 0)
  {
	return false;
  }

  bool
    isNoValue;
  
  vec2
    redMinMax = GetRedMinMaxFloat(iMin, iMax, isNoValue);

  float
    rLow = redMinMax.x,
    rHigh = redMinMax.y;

  float
    rLowNormalized =  (rLow + rOffset)   * rScale,
    rHighNormalized = (rHigh  + rOffset) * rScale; 

  rLowNormalized = max (0.0f, rLowNormalized);
  rLowNormalized = min (1.0f, rLowNormalized);   

  rHighNormalized = max (0.0f, rHighNormalized);
  rHighNormalized = min (1.0f, rHighNormalized);

  if (isNoValue)
  {
    if (nIsNoValueColorVisible != 0)
    {
      return false;
    }
  }


  return IsInvisibleRangeScan(rLowNormalized, rHighNormalized);
}
