uniform sampler2D RTBlurH; // this should hold the texture rendered by the horizontal blur pass
uniform sampler2D Mask;    // Mask used to early-out the blur.

uniform float blurSize;

vec4 lerp(float alpha, vec4 a, vec4 b) {
  return (1.0-alpha) * a + alpha * b;
}
 
void main(void)
{
   vec4 sum = vec4(0.0);
   vec2 tex = gl_TexCoord[0].xy;
   
   vec4 m = texture2D(Mask, tex);
   
   // Early out if the mask is empty.
   if(m[3] < .001) {
     discard;
      // to debug the mask:
      // gl_FragColor = vec4(0,0,0,1);
   } else {
   
     float coefs[9];
     coefs[0] = 0.05;
     coefs[1] = 0.09;
     coefs[2] = 0.12;
     coefs[3] = 0.15;
     coefs[4] = 0.16;
     coefs[5] = 0.15;
     coefs[6] = 0.12;
     coefs[7] = 0.09;
     coefs[8] = 0.05;
   
     // blur in y (vertical)
     // take nine samples, with the distance blurSize between them
     for(int i=0;i<9;++i) {
       vec2 p = vec2(tex.x, tex.y - (float(i)-4.0)*blurSize);
       
       sum += coefs[i]
            * texture2D(RTBlurH, p);
     }
   
     gl_FragColor = sum;
   
   }
}