uniform sampler2D myIntensity;
uniform sampler2D myRGBAMask;
uniform sampler2D myRGBAInput;
uniform sampler2D myDepthInput;

uniform vec4     myColor0;
uniform vec4     myColor1;
uniform vec4     myColor2;
uniform vec4     myColor3;

 vec4 PixelOver(vec4 a, vec4 b)
{
  // pre-multiply
  a.rgb *= a.a;
//  b.rgb *= b.a;

  vec4 c = a + b * (1.0 - a.a);

  // unpre-multiply
  /*
  if (abs(c.a) > 0.001)
    c.rgb /= c.a;
  else
    c = vec4(0.0);
*/
  return c;
}

void main ()                                          
{ 
  vec4
    cI = texture2D(myIntensity, gl_TexCoord[0].xy);
    
  cI = pow(cI, vec4(gl_TexCoord[1].z));
  cI *= vec4(gl_TexCoord[1].w);  
 
  vec4
    cBlurScale = vec4(1.0) - texture2D(myRGBAMask, gl_TexCoord[1].xy);

  vec4
    cBloomInt = min(vec4(1.0), cI * cBlurScale); //vec4(gl_Color.rgb, min(1.0, cI * rBlurScale));
    
  vec4
    cBloom = vec4(0.0);

  cBloom = PixelOver(vec4(myColor3.rgb, myColor3.a * cBloomInt.a), cBloom);
  cBloom = PixelOver(vec4(myColor2.rgb, myColor2.a * cBloomInt.r), cBloom);
  cBloom = PixelOver(vec4(myColor1.rgb, myColor1.a * cBloomInt.g), cBloom);
  cBloom = PixelOver(vec4(myColor0.rgb, myColor0.a * cBloomInt.b), cBloom);

  gl_FragColor = PixelOver(cBloom, texture2D(myRGBAInput, gl_TexCoord[0].zw));
  gl_FragDepth = texture2D(myDepthInput, gl_TexCoord[0].zw).x;
}                                                  
