Search code examples
glslfragment-shaderopengl-es-1.1

Uniform versus attributes in GLSL ES


I have some parameters being passed from CPU to GPU that are constant for all fragments but which change on every frame (I'm using GLSL ES 1.1). Should I use uniforms or attributes for such values? Attributes can vary from vertex to vertex so my intuition is that using attributes for values that are constant across the entire frame would be inefficient. However, I've read that uniforms are for values which change "relatively infrequently", suggesting that changing uniforms on every frame might be inefficient.

In terms of hardware, I'm most interested in optimizing for the iPhone 4S.


Solution

  • I vote for uniforms.

    One of the reasons is already explained in your question: uniforms are constants for each vertex/fragment.

    Other reasons to prefer uniforms against attributes would be:

    • the number of available slots (you are allowed 16 attributes, but many more uniforms)
    • GLSL compilers can optimize uniform value handling
    • less data streaming on long vertex arrays, less attribute per vertex means better performance.
    • uniforms are available in fragment shaders, without defining varying attributes between vertex and fragment shaders
    • uniforms can be structures and arrays, leading to more readable code.