GLSL performance with If and Discard?

Hello, I finally started taking lessons with shading language and the first shader I came up with was discarding normal faces that is 90 degrees away or more from the view, the purpose is to render less geometry and the idea is somewhat like back face culling except it culls the front face you can’t see(unless through reflection), its actually just something I threw together from other example files

import bge

cont = bge.logic.getCurrentController()

VertexShader = """
         varying vec3 varyingNormalDirection; 
            // normalized surface normal vector
         varying vec3 varyingViewDirection; 
            // normalized view direction 
                             
         void main()
         {                
            varyingNormalDirection = 
               normalize(gl_NormalMatrix * gl_Normal);
            varyingViewDirection = 
               -normalize(vec3(gl_ModelViewMatrix * gl_Vertex));

            gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
         }
"""

FragmentShader = """
         varying vec3 varyingNormalDirection; 
            // normalized surface normal vector
         varying vec3 varyingViewDirection; 
            // normalized view direction                       
         const vec4 color = vec4(0.91, 0.91, 0.11, 0.1);

         void main()
         {
            vec3 normalDirection = normalize(varyingNormalDirection);
            vec3 viewDirection = normalize(varyingViewDirection);
            float viewDot = (abs(dot(viewDirection, normalDirection)));
            
            if (viewDot< 0.0)
            {
               discard;
            }
         }
"""

mesh = cont.owner.meshes[0]
for mat in mesh.materials:
    shader = mat.getShader()
    if shader != None:
        if not shader.isValid():
            shader.setSource(VertexShader, FragmentShader, 1)
            mat.setBlending(bge.logic.BL_SRC_ALPHA, 
                            bge.logic.BL_ONE_MINUS_SRC_ALPHA)

Although the brutally pieced together code works fine but many optimization tips out there seems to advice against placing if or discard inside the fragment shader as they are slowing the otherwise parallel operations that the gpu is designed to be

So my main question is would rendering less geometry be beneficial or even outweighs the given cost of badly placed if/discard?

Using discard in the fragment shader wont render any less geometry. You might be able to save on fragment processing by discarding early in the shader. Having the discard at the end of the shader will not really save you anything on performance.

As for branching performance, the important thing to consider is divergence. Branching off of a value that is the same across shader invocations (e.g. constants, defines, uniforms) is usually not much of a problem. Branching off of a value that can differ across shader invocations (e.g. texture look up results, interpolated vertex shader output) can be a problem. Once the instructions are not the same across multiple fragments, the GPU has to do more work to handle things correctly.

Thanks for the explanations, that’s very helpful with how to work with “if” and I will start looking into more info on discarding before the fragment shader runs