I'm working on a WebGL program which implements shadow mapping. After computing the depth values at each fragment, I have an if statement at the end which computes the lighting based on whether the fragment is in a shadow or not.
gl_FragColor = vec4(calcLighting(light0, eye_dir, eye_dir_norm, normal), 1);
if (light_tex_coord.z - depth <= 0.0) {
gl_FragColor += vec4
( calcLighting(light1, eye_dir, eye_dir_norm, normal)
, 0.0
);
}

where depth is the depth of the shadow map and light_tex_coord is the fragment's position in the scene space of light1. light1 is a point light rotating around the model, and light0 is a point light statically positioned at the camera.
The issue here is that the if branch is never taken, so the scene only has light0 applied to it. I've checked that depth, light_tex_coord, and calculateLighting work correctly.
Here's the strange thing, though, replacing the above with the following code:
if (light_tex_coord.z - depth <= 0.0) {
gl_FragColor = vec4(0,1,0,1);
} else {
gl_FragColor = vec4(1,0,0,1);
}

Causes shadowed areas to be correctly drawn in red, and unshadowed to be drawn in green. That is, the branch is correctly evaluated. Replacing it with this:
gl_FragColor = vec4(calcLighting(light0, eye_dir, eye_dir_norm, normal), 1);
gl_FragColor += vec4
( calcLighting(light1, eye_dir, eye_dir_norm, normal)
, 0.0
);

Causes lighting to be correctly computed (sans shadows, though). It seems that when I call the more expensive calcLighting function in the if statement, it doesn't even bother taking it.
Further, I've tried applying the lighting in several ways including using the clamp function; always doing the addition and using a terinary operator to multiply the second calcLighting call by 1 or 0, and arranging my if statement in different ways. Nothing seems to work.
Is there something I'm missing about how branching works in webgl?
vec4variable and only assigninggl_FragColorthe value of this other variable one time. Writing togl_FragColormultiple times in a shader is supposed to be supported as is reading the value of it (which is necessary to implement+=) but by all accounts this issue seems to be related to one or both of these actions given your control flow. - Andon M. Colemangl_FragColorin the above snippets with an accumulatorvec4 acc;. I only assigned at the end of the shader withgl_FragColor = acc;No change in any of the above cases. - Cookytgl_FragColor += vec4( calcLight(..), 1.0 )? [note 1.0 alpha value] - Abstract Algorithm