2

When specifying the vertex attribute location in the shader code using layout(location = ...) I do not need to fetch the locations using glGetAttribLocation in my C++ program.

If I neither define the location in the shader using the layout qualifiers nor fetch them in my C++ program, they are assigned automatically. The question is about this automatic assignment. Does the order equal the order of definition in the shader code?

For example, in first shader code, is the location guaranteed to be the same as in the second shader code?

// first shader code
#version 330

in vec3 position;
in vec3 normal;
in vec2 texcoord;


// second shader code
#version 330

layout(location = 0) in vec3 position;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec2 texcoord;

Moreover, does the same rule apply to fragment shader outputs? For now I use glBindFragDataLocation to fetch them.

danijar
  • 32,406
  • 45
  • 166
  • 297

3 Answers3

3

Automatic attribute assignments are arbitrary; they follow whatever algorithm that the implementation chooses for them to.

If you didn't assign an attribute location, then you cannot assume anything about it.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
2

No -- at least one driver I'm aware of sorts the attributes into alphabetical order before assigning locations to them if they don't have explicit layout directives. In addition, any attribute that is unused in the program will almost certainly be left out and not assigned a location.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
1

You can find out the values that were assigned to your attributes by using a debugger:

positionAttribute = glGetAttribLocation(shaderProgram, "position");
ronm333
  • 131
  • 1
  • 9