Shader input

Shaders take input in two forms: vertex attributes and uniform values. Vertex attributes are unique per vertex and fetched from GPU memory depending on the vertex being processed. Uniform values are set and remain constant for large groups of vertices.

Attributes

Each vertex has associated attributes (color, position, anything!). All attributes are uploaded to the GPU before any rendering takes place. At render, we use glVertexAttribPointer to control how the attributes are used. The GPU will then process the vertices, iterating through all vertices, reading attributes from memory based on vertex ID.

Attribute host syntax

This should be familiar:
glGenBuffers(..) //make buffer
glBindBuffer(..) //activate buffer object
glBufferData(..) //upload dataglGetAttribLocation
..
glGetAttribLocation(..) //bind shader input to buffer
glEnableVertexAttribArray(..) //enable the attribute
glVertexAttribPointer(..) //configure buffer

Attribute device syntax

in vec2 position;
in vec3 color;

Uniforms

Uniforms are small and cheap to upload, since they do not change per vertex, but stay constant over many vertices. Uniforms can be used to drive change in shader programs. For example, it is easy to control animation using a time variable. Time can be uploaded to the GPU as a single float.

Uniform syntax on host

slotId = glGetUniformLocation(shaderHandle, "slot_name");
glUniform1f(slotId, float_val);

When uploading a uniform, you must specify a count, a type, and optionally a vector flag:
glUniform{1|2|3|4}{f|i|ui}[v](location, inputs)

Two was of uploading a 2 component float vector:

vector2 val; //a vector2 on the host

glUniform2f(loc, val.x, val.y); //upload as 2 floats
glUniform2fv(loc, &val, 1); //upload as a 2 float vector

Uniform syntax on device

The shader must be prepared to accept input from the host. Variables with the uniform qualifier are expected to be bound on the host and are set to 0 if not bound. The type of host call to glUniform must match the type expected in the shader. Example for a 2 component float vector:
uniform vec2 v;

Review

Attributes

Uniforms