OpenGL ES Frag Shader - Branching Working on optimizing a fragment shader which is slowing down my OpenGL ES 2.0 shader based game. This is used with a shadow buffer to determine if the fragment/pixel is inside a shadow volume.

The problem/slowdown is the fact I need a branch if or function to determine a 1.0 (no shadow) or 0.0 (shadowed) based on comparison of the frag's shadow Z and shadow buffer Z.

    // SLOWEST
    if ( shadowValue &gt; shadowTexZ ) return 1.0; else return 0.0;

    // ALSO SLOW
    return clamp( 1.0 + (shadowValue - shadowTexZ) * 1000.0, 0.0, 1.0 );

    // SLIGHT LESS SLOW
    return max(sign(shadowValue - shadowTexZ), 0.0);

    // ALSO SLOW
    return sign( shadowValue - shadowTexZ ) * 0.5 + 0.5;


If I disable the shadow test all together, GPU time in Xcode is 12ms and solid 60fps.  With the test on it jumps to 26ms and drops the framerate unacceptably to 40fps.

Thanks!
I think the most efficient way might be either

return float(shadowValue &gt; shadowTexZ);


or

return step(shadowTexZ, shadowValue);