We read:

Attributes (attribute) is a property of the vertex. Vertices may have different attributes. For example, the position coordinates in space, the coordinates of the normal vector, color. In addition, you can pass any attributes to the vertex shader. It is important to understand that an attribute is a property of a vertex, and therefore it must be defined for each vertex. Attributes are passed to only the vertex shader. Attributes are accessible only by the vertex shader and cannot be overwritten.

Uniforms (uniform) are external data that can be used for calculations, but cannot be overwritten. Uniforms can be transferred to both vertex and fragment shaders. Uniforms have nothing to do with a particular vertex and are global constants. For example, as a uniform, you can transfer to the shader the coordinates of the light source and the coordinates of the eye (camera).

For the first:

Attributes are accessible only by the vertex shader and cannot be overwritten.

For the second:

Uniforms (uniform) are external data that can be used for calculations, but cannot be overwritten.

Conclusion:

both can be read but not overwritten.

Question:

How do they differ?

    2 answers 2

    I'll add some more examples:

    For uniform these are global, like lighting parameters, model textures, any coefficients. They are visible in all shaders compiled into the program.

    For attributes it is color, normal, uv, position, tangent, etc.

      A uniform is a constant passed by the programmer to a shader, where it will be used as a constant.

      The attribute is a vertex constant. The attribute will be used to calculate the properties of the vertex, for example, to calculate the texture coordinates of the vertex, then these calculated data will be interpolated for use in the fragment shader.

      Each vertex has its own attributes that are used in the vertex shader, and the uniforms are just constants that are used in both shaders. And just the variation is the calculated value for the vertex, which in the fragment shader will be interpolated depending on the distance to the vertices.

      The programmer transmits the data for the vertices in the attributes - coordinates, texture coordinates, normal, color, in general, in fact, anything else. In the shader, this will look like one variable, for example, a color, but - in reality, for each vertex, the values ​​for this vertex will be entered into the attribute, and the vertex shader will be called. If you look at the vertex shader as a function / method, then the attributes are the parameters of this function, and the function itself is called by the video card for each vertex.

      The task of the programmer is to upload attributes for each vertex to the video card. That is, we have, for example, 20 vertices, so it is necessary to transfer 20 coordinates, 20 vertex coordinates, 20 normals, etc. (all that is needed for a particular shader). The graphics card gets these arrays. At the beginning of the drawing, vertices are "counted". For each vertex, the corresponding attribute values ​​are taken from the arrays and are substituted into the vertex vertex, it is executed, and it performs calculations for a specific vertex based on its attributes.

      • why not use uniforms with an appropriate type as an attribute and not calculate everything else from it? Why such specialization? - perfect
      • In simple terms, the attribute is normal, position, vertex color, i.e. they are per-vertex. The uniform is a global constant for the entire shader program, in other words, the per-shader program. Attributes are usually stored in a buffer on a video card, and when drawn, they are simply pulled into the shaders, i.e. There is no load on the tires, and the uniforms are set through the code executed on the processor. Uniforms can be used for color / direction of color source, texture etc. - selya
      • Rewrote, it seems it has become more accessible - markov