OpenGL Shading Language Different Types of Variable (Qualifiers)

fragment-shaderglslopenglshadervertex

I've been writing programs using OpenGL. Recently, I started learning OpenGL Shading Language. I'm a newbie; so please be detailed in your answers.

My questions are:

  1. What are different types of variable (qualifiers) in GLSL?
  2. What are they used for?
  3. How are they different from one another?

I am only familiar with "varying" variable which is passed from Vertex Shaders to Fragment Shaders to be interpolated between vertices. Other than that, I know nothing else.

Best Answer

In OpenGL 3+ :

  • varying is deprecated
  • const is for... well, constants !
  • uniform is for per draw call (at most) values
  • in is for input from the previous pipeline stage, i.e. per vertex (or per fragment) values at most, per primitive if using glAttribDivisor and hardware instanciation
  • out is for output to the next stage

Regarding outputs for fragment shaders : in OpenGL3 and up, most of the built-in variables for fragment shader output (such as gl_FragColor, with the notable exception of gl_FragDepth) are deprecated and should be replaced with user-defined out variables.

If you are outputting to the default framebuffer, whatever you declare as the output of the fragment shader ends up in the color buffer. If you've bound an FBO with multiple color buffers (i.e. multiple render targets), you'll need to manually bind each of your out variables to the correct color buffer index via glBindFragDataLocationIndexed.

All the details you could ever want about both the GLSL ('server') side and the OpenGL ('client') side can be found :

Related Topic