How does the Vertex Shader receives data?

Today I want to talk about how a Vertex Shader receives data. If you recall a Vertex Shader is the first stage in the graphics pipeline. It computes and passes forward the coordinates of a model to the different shaders. But how does the Vertex Shader receives data?

Let's talk about some key points:

  • The Vertex Shader is the only mandatory stage in the graphics pipeline. (It makes no sense in only having a vertex shader, but it is the only mandatory one).
  • Before the Vertex Shader runs, a fixed-function stage known as Vertex Fetch executes. This stage fetches data from your application.

In GLSL, the mechanism for getting data in and out of a shader is to declare them as in or out storage qualifiers. When you declare a variable as in, it marks it as an input to the vertex shader. This sets the variable as an input to the graphics pipeline.

In OpenGL, this in variable is known as a Vertex Attribute.

It is through Vertex Attributes that vertex data are introduced into the graphics pipeline.

We can tell the Vertex-Fetch stage what data to fill the attribute with by using glVertexAttrib().

void glVertexAttrib4fv(GLuint index, const GLFloat *v);

The parameter index references the attribute and v is a pointer to the data to put into the attribute.

You may have noticed the following declaration in many vertex shaders:

layout (location =0);

The layout qualifier is used to set the location of the vertex attribute. This location is the value that you use as index to refer to the attribute.

If you want to update the value of the vertex attribute in the shader, simply update the data being pointed by glVertexAttrib().

So how does the Vertex Shader receives data? It receives data by making use of the Vertex-Fetch stage, in-qualifier variables and glVertexAttrib().

PS. Sign up to my newsletter and get OpenGL development tips.

Harold Serrano

Computer Graphics Enthusiast. Currently developing a 3D Game Engine.