13

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.

Laurel
  • 5,965
  • 14
  • 31
  • 57
Einiosaurus
  • 191
  • 2
  • 12
  • 1
    Thank you all for your answers. From some online sources, I found information about two qualifiers "in" and "out", which can also be used for Vertex - Fragment Shaders communication. They are very similar to "varying" qualifier. Are they a separate "class" of qualifiers or just some alternate to "varying" qualifier? – Einiosaurus Jun 01 '11 at 05:09

3 Answers3

21

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 :

Nicolas Lefebvre
  • 4,247
  • 1
  • 25
  • 29
  • Great, Thanks a lot! Just one small question, what is the next stage of a Fragment Shader? If your Fragment Shader defines a "out" variable, where the value of that variable is passed to? – Einiosaurus Jun 01 '11 at 08:33
  • Edited my answer to clarify the issue of fragment shader output ! – Nicolas Lefebvre Jun 01 '11 at 09:10
  • "what is the next stage of a Fragment Shader?" -> none in 2011, but everyone expects "blend shaders" instead of ugly glBlendFunc. EDIT : +1 for quoting specs with exact pages. – Calvin1602 Jun 01 '11 at 19:08
  • Since which version did gl_FragColor became deprecated? thank you – André Puel Feb 20 '13 at 13:15
2
  • Uniform: Constant data per draw call, usually used to parametrize shaders.
  • Varying: Transfers data between the VS and the FS, interpolating along the primitive in the process.
  • Attribute: This is vertex attribute data, the input of the Vertex Shader. It's specified for each vertex in the primitive.
Dr. Snoopy
  • 55,122
  • 7
  • 121
  • 140
1

Check the tutorial: http://www.lighthouse3d.com/tutorials/glsl-tutorial/

there are Uniform Variables(static for all thread)

Attribute Variables (injected per-vertex)

varying (as you know)

demaxSH
  • 1,743
  • 2
  • 20
  • 27