Skip to content
systemcoding.dev
GPU

Declare vertex inputs with layout(location = 0) in

Declare vertex inputs with layout(location = 0) in so the shader fixes attribute locations itself, available in GLSL 3.30 and OpenGL 3.3.

1 min read
Summarize in AI

Without a layout qualifier, the linker picks attribute locations and you must ask for them with glGetAttribLocation. Writing the location in the shader makes the C side a constant.

mesh.vertglsl
#version 330 core
layout(location = 0) in vec3 a_pos;
layout(location = 1) in vec2 a_uv;
layout(location = 2) in vec3 a_normal;

out vec2 v_uv;
uniform mat4 u_mvp;

void main() {
    v_uv = a_uv;
    gl_Position = u_mvp * vec4(a_pos, 1.0);
}
attribs.cc
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 32, (void *)0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 32, (void *)12);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 32, (void *)20);

The layout(location = N) form for vertex inputs is core in GLSL 3.30, which OpenGL 3.3 uses, as the GLSL specification defines. Older versions need the extension ARB_explicit_attrib_location. A mat4 input takes four consecutive locations, so the next input after it must skip three numbers.

Two inputs sharing a location is a link error, which is better than the silent mix-up of automatic assignment.

Share

More snippets

GPU

List platforms with clGetPlatformIDs

Call clGetPlatformIDs twice, once for the count and once for the handles, then print each platform name with clGetPlatformInfo in OpenCL 3.0 C.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.