varying variables take on a unique value per fragment. In this example, that's a per fragment surface normal and a per-fragment texture coordinate value.
uniform variables have the same value for all fragments. In this example, the uniform variables include the direction the light source is pointing as well as the texture map that is sampled in the fragment shader.
kayvonf
Question: Notice that there's no for loop in this code that loops over all fragments. This is an interesting design decision for the language. Give me a few reasons why graphics shading languages enforce a style of programming where the programmer defines only the logic to perform on a single fragment (or a single vertex, if this was a vertex shader).
In other words, the OpenGL/Direct3D graphics pipeline defines what the "outer loops" of the program are, and the application programmer provides the code that fills in the body of the inner loop. Note how this is the opposite of how libraries are often designed, where the application developer fills in the global structure of the code, and uses highly optimized "black box libraries" for the body of an inner loop. (e.g., that's how you'd program if you were using MATLAB.)
lucida
One reason for only allowing the programmer to define the logic to perform on a single fragment is that APIs like OpenGL are cross platform and handle the actual implementation, scheduling, and parallelization of these tasks to the GPU for the programmer.
This is preferred because it abstracts away from the graphics programmer the need to know platform specific details when writing a graphics program. They can write a general program that through OpenGL can run on multiple platforms.
varying
variables take on a unique value per fragment. In this example, that's a per fragment surface normal and a per-fragment texture coordinate value.uniform
variables have the same value for all fragments. In this example, the uniform variables include the direction the light source is pointing as well as the texture map that is sampled in the fragment shader.Question: Notice that there's no
for
loop in this code that loops over all fragments. This is an interesting design decision for the language. Give me a few reasons why graphics shading languages enforce a style of programming where the programmer defines only the logic to perform on a single fragment (or a single vertex, if this was a vertex shader).In other words, the OpenGL/Direct3D graphics pipeline defines what the "outer loops" of the program are, and the application programmer provides the code that fills in the body of the inner loop. Note how this is the opposite of how libraries are often designed, where the application developer fills in the global structure of the code, and uses highly optimized "black box libraries" for the body of an inner loop. (e.g., that's how you'd program if you were using MATLAB.)
One reason for only allowing the programmer to define the logic to perform on a single fragment is that APIs like OpenGL are cross platform and handle the actual implementation, scheduling, and parallelization of these tasks to the GPU for the programmer.
This is preferred because it abstracts away from the graphics programmer the need to know platform specific details when writing a graphics program. They can write a general program that through OpenGL can run on multiple platforms.