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.
- 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?
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 aglDraw
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.