Example/Dynamically Uniform Expression

From OpenGL.org
Jump to: navigation, search

This example code shows what are and are not dynamically uniform expressions.

in vec3 fromPrevious;
in uvec2 fromRange;
 
const int foo = 5;
const uvec2 range = uvec2(2, 5);
uniform vec2 pairs;
 
uniform sampler2d tex;
 
void main()
{
  foo; //constant expressions are dynamically uniform.
 
  uint value = 21; //value is dynamically uniform.
  value = range.x; //still dynamically uniform.
  value = range.y + fromRange.y; //not dynamically uniform; current value comes from a non-dynamically uniform source.
  value = 4; //dynamically uniform again.
  if(fromPrevious.y < 3.14)
    value = 12;
  value; //NOT dynamically uniform. The value of this variable depends on fromPrevious, an input variable.
 
  float number = abs(pairs.x); //number is dynamically uniform.
  number = sin(pairs.y); //still dynamically uniform.
  number = cos(fromPrevious.x); //not dynamically uniform.
 
  vec4 colors = texture(tex, pairs.xy); //dynamically uniform, even though it comes from a texture.
                                        //It uses the same texture coordinate, thus getting the same texel every time.
  colors = texture(tex, fromPrevious.xy); //not dynamically uniform.
 
  for(int i = range.x; i < range.y; ++i)
  {
       //loop initialized with, compared against, and incremented by dynamically uniform expressions.
    i; //Therefore, it is dynamically uniform.
  }
 
  for(int i = fromRange.x; i < fromRange.y; ++i)
  {
    i; //Not dynamically uniform; `fromRange` is not dynamically uniform.
  }
}