Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 217
Alex Flint Avatar asked Jun 22 '12 14:06

Alex Flint


People also ask

What is the main difference between and attribute and a uniform variable?

The difference between attribute and uniform variable is that attribute variables contain data which is vertex specific so they are reloaded with a new value from the vertex buffer for each shader invocation while the value of uniform variables remains constant accross the entire draw call.

What does uniform mean in GLSL?

A uniform is a global Shader variable declared with the "uniform" storage qualifier. These act as parameters that the user of a shader program can pass to that program. Their values are stored in a program object.

What is the difference between a uniform variable and a varying variable?

uniform are per-primitive parameters (constant during an entire draw call) ; attribute are per-vertex parameters (typically : positions, normals, colors, UVs, ...) ; varying are per-fragment (or per-pixel) parameters : they vary from pixels to pixels.

What is GLSL attribute?

Attributes are GLSL variables which are only available to the vertex shader (as variables) and the JavaScript code. Attributes are typically used to store color information, texture coordinates, and any other data calculated or retrieved that needs to be shared between the JavaScript code and the vertex shader.


1 Answers

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.
like image 199
Luca Avatar answered Sep 24 '22 17:09

Luca