<p>Each VAO, and shader for that matter, has a separate space of attribute locations. It's perfectly fine to have multiple shaders use the same attribute locations. Indeed, it's a good idea to do so if those attributes conceptually mean the same thing to both shaders.</p>

<p>A VAO can be used with a shader if that VAO's attribute locations match those the program expects. Well, technically they don't have to match. It's OK if the VAO provides an attribute that a shader doesn't use. <a href="https://www.opengl.org/wiki/Vertex_Specification#Non-array_attribute_values" rel="nofollow noreferrer">It's more complicated</a> if a VAO doesn't provide an attribute for a location that the shader expects.</p>

<p>So whatever it is that generates your shader prefix strings and VAOs should not be based on what has been generated for other shaders/VAOs. So if you're going to have attributes increase numerically starting from 0, then this should be the case for each independent shader. It should not be based on some constantly increasing global.</p>

CC BY-SA 3.0
2016-11-02T09:41:51.020Z
Thanks! And you are right, the OpenGL-Class has a static member `static GLuint NumberAddedAttributes` which is increased every time an attribute is added. And if each VAO really has its own attribute space, this would mean that i only have to make this member non-static and everything should work. But when i tried this yesterday, i ran into some problems. But now since I read your answer I'll check it again.
Christian_B
CC BY-SA 3.0
2016-11-02T10:04:27.940Z
I would like to extent the question to SSBO (shader storage buffer object): I also use them and create the shader preamble automatically. I guess in case of SSBO binding points the counter has to be global for all shader?
Christian_B
CC BY-SA 3.0
2016-11-02T03:35:03.927Z
2016-11-02T03:35:03.927Z
Nicol Bolas
<p>I'm trying to switch between the rendering of two different scenes as fast as possible using OpenGL.</p>

<p>For all my OpenGL related stuff I wrote a wrapper-class which provides a quite easy way of adding and updating data. Based on the added data it automatically creates the variable naming for the shader. In the past I always used just one instance of this OpenGL class but now I'm trying to use two of them. (Each instance has its own shader program.)</p>

<p>When I do something like:</p>

<pre><code>OpenGLInstance1.AssignModel(*MyModel1);
</code></pre>

<p>it automatically adds data and writes into the shader code:</p>

<pre><code>// per vertex attributes 
layout (location = 0) in vec3   position;
layout (location = 1) in vec3   normals;
layout (location = 2) in uvec4  BoneIDs;
layout (location = 3) in vec4   Weights;

// per instance attributes
layout (location = 4) in mat4   CameraMatrix;           //dynamic
layout (location = 8) in mat4   ProjectionMatrix;       //dynamic
</code></pre>

<p>When I try to do this a second time with an other OpenGL-Class instance and Model:</p>

<pre><code>OpenGLInstance2.AssignModel(*MyModel2);
</code></pre>

<p>I get:</p>

<pre><code>// per vertex attributes 
layout (location = 12) in vec3  position;
layout (location = 13) in vec3  normals;
layout (location = 14) in uvec4 BoneIDs;
layout (location = 15) in vec4  Weights;

// per instance attributes
layout (location = 16) in mat4  CameraMatrix;           //dynamic
layout (location = 20) in mat4  ProjectionMatrix;       //dynamic
</code></pre>

<p>But since the GL_MAX_VERTEX_ATTRIBS gives a maximum number of attributes of 16, this is not possible.</p>

<h3>Question 1</h3>

<p>Each VAO (vertex array object) has some VBO (vertex buffer object) assigned. Is the location indexing of the VBO just in the scope of each VAO (starting again from zero for each VAO) or is it independent of the VAO?</p>

<h3>Question 2</h3>

<p>Do you have a suggestion how to work around this problem while keeping the implementation as generic, reusable and scalable as possible? (Just some general hints, I don't want to bother you with details.)</p>

CC BY-SA 3.0
2016-11-01T15:04:41.280Z
I would suggest using uniforms for most of these parameters, such as transformation matrices. You can use uniform buffers to quickly switch uniforms when rendering. Also, the vao just stores the attribute bindings and not the vbo itself.
vgs
<opengl><shader><glsl><vertex-buffer-object>
Attribute Location in Multiple Shader Programs
Christian_B
# Attribute Location in Multiple Shader Programs

(Asked by: username_0 on 2016-11-01)

I'm trying to switch between the rendering of two different scenes as fast as possible using OpenGL.

For all my OpenGL related stuff I wrote a wrapper-class which provides a quite easy way of adding and updating data. Based on the added data it automatically creates the variable naming for the shader. In the past I always used just one instance of this OpenGL class but now I'm trying to use two of them. (Each instance has its own shader program.)

When I do something like:

```
OpenGLInstance1.AssignModel(*MyModel1);

```

it automatically adds data and writes into the shader code:

```
// per vertex attributes 
layout (location = 0) in vec3   position;
layout (location = 1) in vec3   normals;
layout (location = 2) in uvec4  BoneIDs;
layout (location = 3) in vec4   Weights;

// per instance attributes
layout (location = 4) in mat4   CameraMatrix;           //dynamic
layout (location = 8) in mat4   ProjectionMatrix;       //dynamic

```

When I try to do this a second time with an other OpenGL-Class instance and Model:

```
OpenGLInstance2.AssignModel(*MyModel2);

```

I get:

```
// per vertex attributes 
layout (location = 12) in vec3  position;
layout (location = 13) in vec3  normals;
layout (location = 14) in uvec4 BoneIDs;
layout (location = 15) in vec4  Weights;

// per instance attributes
layout (location = 16) in mat4  CameraMatrix;           //dynamic
layout (location = 20) in mat4  ProjectionMatrix;       //dynamic

```

But since the GL\_MAX\_VERTEX\_ATTRIBS gives a maximum number of attributes of 16, this is not possible.

### Question 1

Each VAO (vertex array object) has some VBO (vertex buffer object) assigned. Is the location indexing of the VBO just in the scope of each VAO (starting again from zero for each VAO) or is it independent of the VAO?

### Question 2

Do you have a suggestion how to work around this problem while keeping the implementation as generic, reusable and scalable as possible? (Just some general hints, I don't want to bother you with details.)

**Tags:** opengl, shader, glsl, vertex-buffer-object

> **Comments:**
> **username_1**: I would suggest using uniforms for most of these parameters, such as transformation matrices. You can use uniform buffers to quickly switch uniforms when rendering. Also, the vao just stores the attribute bindings and not the vbo itself.  

---

## 1 answers

### Answer by username_2 on 2016-11-02. Score: 3 | Accepted answer

Each VAO, and shader for that matter, has a separate space of attribute locations. It's perfectly fine to have multiple shaders use the same attribute locations. Indeed, it's a good idea to do so if those attributes conceptually mean the same thing to both shaders.

A VAO can be used with a shader if that VAO's attribute locations match those the program expects. Well, technically they don't have to match. It's OK if the VAO provides an attribute that a shader doesn't use. [It's more complicated](https://www.opengl.org/wiki/Vertex_Specification#Non-array_attribute_values) if a VAO doesn't provide an attribute for a location that the shader expects.

So whatever it is that generates your shader prefix strings and VAOs should not be based on what has been generated for other shaders/VAOs. So if you're going to have attributes increase numerically starting from 0, then this should be the case for each independent shader. It should not be based on some constantly increasing global.

> **Comments:**
> **username_0**: Thanks! And you are right, the OpenGL-Class has a static member `static GLuint NumberAddedAttributes` which is increased every time an attribute is added. And if each VAO really has its own attribute space, this would mean that i only have to make this member non-static and everything should work. But when i tried this yesterday, i ran into some problems. But now since I read your answer I'll check it again.  
> **username_0**: I would like to extent the question to SSBO (shader storage buffer object): I also use them and create the shader preamble automatically. I guess in case of SSBO binding points the counter has to be global for all shader?  

---

