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.
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.
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.
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.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With