Is there an analogy that I can think of when comparing these different types, or how these things work?
Also, what does uniforming a matrix mean?
Is there an analogy that I can think of when comparing these different types, or how these things work?
Also, what does uniforming a matrix mean?
I like the description from https://learnopengl.com/Getting-started/Shaders , because the the word per-primitive is not intuitive
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.It's important to understand how
varying
works to program your own shaders.Let's say you define a varying parameter
v
for each vertex of a triangle inside the vertex shader. When this varying parameter is sent to the fragment shader, its value is automatically interpolated through bilinear interpolation based on the position of the pixel to draw.In the following image, the red pixel received an interpolated value of the varying parameter
v
. That's why we call them "varying".In OpenGL, a "program" is a collection of "shaders" (smaller programs), which are connected to each other in a pipeline.
Shaders process vertices (vertex shader), geometries (geometry shader), tessellation (tessellation shader), fragments (pixel shader), and other batch process tasks (compute shader) needed to rasterize a 3D model.
OpenGL (WebGL) shaders are written in GLSL (a text-based shader language compiled on the GPU).
Keeping these concepts in mind:
Shaders can pass data to the next shader in the pipeline (
out
,inout
), and they can also accept data from the WebGL application or a previous shader (in
).The Vertex and Fragment shaders (any shader really) can use a
uniform
variable, to receive data from the WebGL application.The Vertex Shader can also receive data from the WebGL application with the
attribute
variable, which can be enabled or disabled as needed.The Vertex Shader can pass data to the Fragment Shader using the
varying
variable. See GLSL code above (varying vec3 variableC;
).Copied directly from http://www.lighthouse3d.com/tutorials/glsl-tutorial/data-types-and-variables/. The actual site has much more detailed information and would be worthwhile to check out.
As for an analogy, const and uniform are like global variables in C/C++, one is constant and the other can be set. Attribute is a variable that accompanies a vertex, like color or texture coordinates. Varying variables can be altered by the vertex shader, but not by the fragment shader, so in essence they are passing information down the pipeline.