#version 400

uniform sampler2D inputTex;
uniform float     threshold;     

in vec2 texelPos;

out vec4 result;

// This is the actual code that is executed.
void main( void )
{	
	// Read the pixel 
	vec4 texel = texture( inputTex, texelPos );	
	
	// Check if this sample is a residual (stored in w coef)
	bool isResidual = texel.w < 0.5;
	
	// Since residuals are adjusted to center around 0.5, adjust back
	vec3 residual = texel.xyz - 0.5;
	
	// Crop residuals below the threshold
	residual.x = (abs(residual.x) < threshold) ? 0.0 : residual.x;
	residual.y = (abs(residual.y) < threshold) ? 0.0 : residual.y;
	residual.z = (abs(residual.z) < threshold) ? 0.0 : residual.z;
	
	// Output the cropped residual.  Leave non-residuals unchanged
	result = isResidual ? vec4( 0.5+residual.xyz, 0.0 ) : texel;
}