Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 41 down vote accepted

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

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.

share|improve this answer
@NicolBolas Ha! I already figured there was something fishy with that. That's an error in that reference card then. Thanks! :-) – Eric May 26 '12 at 21:36
Thank you very much. – Avi May 26 '12 at 21:57
link is broken !!! – Saleh Sep 11 '15 at 17:16
@Saleh Fixed it, thanks. – Eric Sep 12 '15 at 7:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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