gambit20088
gambit20088

Reputation: 321

Difference between GLSL shader variable types?

When seeing some OpenGL examples, some use the following types of variables when declaring them at the top of the shader:

in out

and some use:

attribute varying uniform

What is the difference? Are they mutually exclusive?

Upvotes: 3

Views: 1493

Answers (2)

Nicol Bolas
Nicol Bolas

Reputation: 474326

attribute and varying were removed from GLSL 1.40 and above (desktop GL version 3.1) in core OpenGL. OpenGL ES 2 still uses them, but were removed from ES 3.0 code.

You can still use the old constructs in compatibility profiles, but attribute only maps to vertex shader inputs. varying maps to both VS outputs and FS inputs.

uniform has not changed; it still means what it always has: values set by the outside world which are fixed during a rendering operation.

Upvotes: 4

Dietrich Epp
Dietrich Epp

Reputation: 213768

In modern OpenGL, you have a series of shaders hooked up to a pipeline. A simple pipeline will have a vertex shader and a fragment shader.

For each shader in the pipeline, the in is the input to that stage, and the out is the output to that stage. The out from one stage will get matched with the in from the next stage.

A uniform can be used in any shader and will stay constant for the entire draw call.

If you want an analogy, think of it as a factory. The in and out are conveyor belts going in and out of machines. The uniform are knobs that you turn on a machine to change how it works.

Example

Vertex shader:

// Input from the vertex array
in vec3 VertPos;
in vec2 VertUV;

// Output to fragment shader
out vec2 TexCoord;

// Transformation matrix
uniform mat4 ModelViewProjectionMatrix;

Fragment shader:

// Input from vertex shader
in vec2 TexCoord;

// Output pixel data
out vec4 Color;

// Texture to use
uniform sampler2D Texture;

Older OpenGL

In older versions of OpenGL (2.1 / GLSL 1.20), other keywords were used instead of in and out:

  • attribute was used for the inputs to the vertex shader.

  • varying was used for the vertex shader outputs and fragment shader inputs.

  • Fragment shader outputs were implicitly declared, you would use gl_FragColor instead of specifying your own.

Upvotes: 4

Related Questions