1

I understand that some modern implementations of OpenGL ES optimize branches that are taken from a uniform value to avoid thread divergence and I have a few questions about this.

  1. If I was to take a branch based off of a comparison of a uniform variable or even between two uniform variables, could this branch still be optimized to avoid thread divergence? To what extent do constant expressions get folded in these cases by GLSL?
  2. Does this property carry over to members of structs accessed from a uniform buffer object? Say I had a uniform global variable index defining the index into an array of structs related to a running shader and all of the fragments over a glDraw call accessed the same data from a uniform block and used one of its members for a conditional branch.

    uniform int u_index;
    struct ShaderData {
        bool condition1;
        bool condition2;
        lowp vec2 value;
    };
    
    layout(std140) uniform u_input {
        ShaderData u_data[64];
    };
    
    void main() {
        ShaderData instance = u_data[u_instance];
        if (instance.condition1) {
            // optimized?
        }
    
        if (instance.condition1 && instance.condition2) {
            // optimized?
        }
    
        if (instance.value.x > instance.value.y) {
            // optimized?
        }
    }
    

NOTE: This question is similar to Does If-statements slow down my shader? but I seek information specifically regarding the use of uniform blocks as opposed to just uniforms, and clarification regarding creating a condition based on the values of uniforms. I've read the other one before asking and it does not answer my questions fully.

Alex Zielenski
  • 3,591
  • 1
  • 26
  • 44
  • To me this sounds like a very driver, version and hence platform specific question whose answer is prone to be outdated/wrong with the next driver update, apart from that you can't throw OpenGL and its ES sibling into the same bucket. – LJᛃ Feb 28 '18 at 18:00
  • "*I seek information specifically regarding the use of uniform blocks as opposed to just uniforms*" Why do you think that changes anything? Do you think a compiler that can tell that a uniform variable is involved in an expression would be unable to do so if that variable is in a uniform block? – Nicol Bolas Mar 01 '18 at 04:26
  • I don't know, maybe the internal organization for uniform blocks is different from simple uniforms and the GPU would be simply unable to do it so the compiler does not optimize? I never read anything that indicated the treatment of the two would be absolutely identical. I never doubted the compilers ability to parse text, for what it's worth and thought it was a question worth asking. Thanks for your professional tone, though. – Alex Zielenski Mar 01 '18 at 15:15

0 Answers0