Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between GLSL shader variable types?

Tags:

opengl

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?

like image 388
gambit20088 Avatar asked Sep 23 '26 11:09

gambit20088


2 Answers

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.

like image 148
Nicol Bolas Avatar answered Sep 26 '26 01:09

Nicol Bolas


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.

like image 41
Dietrich Epp Avatar answered Sep 26 '26 00:09

Dietrich Epp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!