I don't really understand how to use the in / out keywords in GLSL, and google is being uncharacteristically unhelpful.

What exactly do they do? How would I use them if, for example, I want to pass a varying variable set per vertex to the fragment shader?

Literally every tutorial I find uses the varying / attribute keywords and that's not helpful.


The storage qualifiers in and out actually have a purpose that contains and supersedes that of varying and attribute. They define what variables are respectively inputs and outputs for the shader. See the GLSL 4.2 reference card page 7:

  • in: linkage into shader from previous stage
  • out: linkage out of a shader to next stage
  • attribute: same as in for vertex shader
  • varying: same as out for vertex shader, same as in for fragment shader (Note: these are erroneously flipped around in the above-mentioned reference card.)

With the side note that the latter two are sort of deprecated: they are not present in the 4.2 core profile, only in the compatibility profile.

What exactly do they do?

As for usage, take the vertex shader from An intro to modern OpenGL. Chapter 2.2: Shaders:

#version 110

attribute vec2 position;    
varying vec2 texcoord;

void main()
    gl_Position = vec4(position, 0.0, 1.0);
    texcoord = position * vec2(0.5) + vec2(0.5);

It should be rewritten in 4.2 core as:

#version 420

in vec2 position;    
out vec2 texcoord;

void main()
    gl_Position = vec4(position, 0.0, 1.0);
    texcoord = position * vec2(0.5) + vec2(0.5);

Unhelpful Tutorials

I'm guessing the main reason you find "outdated" tutorial code is that not everyone has access to GLSL 3.3+ compatible hardware. Regardless, for a good and more up to date tutorial I'll gladly point you in the direction of Nicol Bolas' Learning Modern 3D Graphics Programming.

  • 2
    \$\begingroup\$ @NicolBolas Ha! I already figured there was something fishy with that. That's an error in that reference card then. Thanks! :-) \$\endgroup\$
    – Eric
    May 26 '12 at 21:36
  • 1
    \$\begingroup\$ Those tutorials from 2012 are outdated by now, with DSA and all that. Better get the Red and Blue book latest editions and (even better) read the specification. Compared to Vulkan, the specifications (OpenGL and GLSL) are a piece of cake. With cream. \$\endgroup\$
    – user144188
    Nov 1 '20 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.