<p>Your code is non-functional, but only because (in the core profile) you cannot bind a buffer to <code>GL_ELEMENT_ARRAY_BUFFER</code> unless a VAO is also bound. So the part that creates the buffer would need to use a different binding point, which is totally fine:</p>

<pre><code>const GLuint indices[6] = {
    0, 1, 2,
    0, 3, 2
};
glGenBuffers (1, &amp;EBO);
glBindBuffer (GL_ARRAY_BUFFER, EBO);
glBufferData (GL_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
</code></pre>

<p>Everything else is perfectly valid.</p>

CC BY-SA 4.0
2018-08-30T13:36:12.017Z
So, there is no way to avoid multiple `glBufferData` calls?
trexxet
CC BY-SA 4.0
2018-08-30T13:51:55.493Z
@trexxet: I don't know what you mean. I said "Everything else is perfectly valid."
Nicol Bolas
CC BY-SA 4.0
2018-08-30T18:01:09.393Z
I asked if it is possible to share single EBO for multiple VAOs to avoid multiple data transfers of same data. However, after your answer I assume that it's not possible.
trexxet
CC BY-SA 4.0
2018-08-30T18:02:28.413Z
@trexxet: "***Everything else is perfectly valid.***" I don't know how you can read that statement and come away with the idea that this is not possible.
Nicol Bolas
CC BY-SA 4.0
2018-08-30T18:27:48.577Z
I misunderstood a bit, but now I get it: bind first VAO, bind EBO, load EBO, bind second VAO, bind EBO, bind third VAO etc., right?
trexxet
CC BY-SA 4.0
2018-08-30T18:42:19.877Z
@trexxet: No. You replace the first half of the code you wrote with the code *I wrote*.
Nicol Bolas
CC BY-SA 4.0
2018-08-29T16:59:27.003Z
2018-08-29T16:59:27.003Z
Nicol Bolas
<p>As mentioned <a href="https://computergraphics.stackexchange.com/questions/4623/multiple-vao-share-a-vbo">here</a>, multiple VAOs can share a single VBO. However, can multiple VAOs share a single EBO?</p>

<p>For example, I want to create 2 rectangles. So, I do the following:</p>

<pre><code>const GLuint indices[6] = {
    0, 1, 2,
    0, 3, 2
};

// First
glGenVertexArrays (1, &amp;VAO1);
glBindVertexArray (VAO1);
glGenBuffers (1, &amp;EBO1);
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, EBO1);
glBufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// creating VBO, setting attributes etc
// Second
glGenVertexArrays (1, &amp;VAO2);
glBindVertexArray (VAO2);
glGenBuffers (1, &amp;EBO2);
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, EBO2);
glBufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
</code></pre>

<p><code>glBufferData(GL_ELEMENT_ARRAY_BUFFER)</code> is called twice for different EBOs with same data. As I understand, this leads to double GPU memory consumption and takes twice loading time. </p>

<p>So, would the following be correct?</p>

<pre><code>const GLuint indices[6] = {
    0, 1, 2,
    0, 3, 2
};
glGenBuffers (1, &amp;EBO);
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

// First
glGenVertexArrays (1, &amp;VAO1);
glBindVertexArray (VAO1);
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, EBO);
// creating VBO, setting attributes etc
// Second
glGenVertexArrays (1, &amp;VAO2);
glBindVertexArray (VAO2);
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, EBO);
</code></pre>

<opengl><buffers>
Multiple VAO share a EBO (OpenGL 3.3)
trexxet
# Multiple VAO share a EBO (OpenGL 3.3)

(Asked by: username_0 on 2018-08-28)

As mentioned [here](https://computergraphics.stackexchange.com/questions/4623/multiple-vao-share-a-vbo), multiple VAOs can share a single VBO. However, can multiple VAOs share a single EBO?

For example, I want to create 2 rectangles. So, I do the following:

```
const GLuint indices[6] = {
    0, 1, 2,
    0, 3, 2
};

// First
glGenVertexArrays (1, &VAO1);
glBindVertexArray (VAO1);
glGenBuffers (1, &EBO1);
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, EBO1);
glBufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// creating VBO, setting attributes etc
// Second
glGenVertexArrays (1, &VAO2);
glBindVertexArray (VAO2);
glGenBuffers (1, &EBO2);
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, EBO2);
glBufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

```

`glBufferData(GL_ELEMENT_ARRAY_BUFFER)` is called twice for different EBOs with same data. As I understand, this leads to double GPU memory consumption and takes twice loading time.

So, would the following be correct?

```
const GLuint indices[6] = {
    0, 1, 2,
    0, 3, 2
};
glGenBuffers (1, &EBO);
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

// First
glGenVertexArrays (1, &VAO1);
glBindVertexArray (VAO1);
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, EBO);
// creating VBO, setting attributes etc
// Second
glGenVertexArrays (1, &VAO2);
glBindVertexArray (VAO2);
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, EBO);

```

**Tags:** opengl, buffers


---

## 1 answers

### Answer by username_1 on 2018-08-29. Score: 1 | Accepted answer

Your code is non-functional, but only because (in the core profile) you cannot bind a buffer to `GL_ELEMENT_ARRAY_BUFFER` unless a VAO is also bound. So the part that creates the buffer would need to use a different binding point, which is totally fine:

```
const GLuint indices[6] = {
    0, 1, 2,
    0, 3, 2
};
glGenBuffers (1, &EBO);
glBindBuffer (GL_ARRAY_BUFFER, EBO);
glBufferData (GL_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

```

Everything else is perfectly valid.

> **Comments:**
> **username_0**: So, there is no way to avoid multiple `glBufferData` calls?  
> **username_1**: @username_0: I don't know what you mean. I said "Everything else is perfectly valid."  
> **username_0**: I asked if it is possible to share single EBO for multiple VAOs to avoid multiple data transfers of same data. However, after your answer I assume that it's not possible.  
> **username_1**: @username_0: "***Everything else is perfectly valid.***" I don't know how you can read that statement and come away with the idea that this is not possible.  
> **username_0**: I misunderstood a bit, but now I get it: bind first VAO, bind EBO, load EBO, bind second VAO, bind EBO, bind third VAO etc., right?  
> **username_1**: @username_0: No. You replace the first half of the code you wrote with the code *I wrote*.  

---

