0
$\begingroup$

As mentioned here, 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);
$\endgroup$
0
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ So, there is no way to avoid multiple glBufferData calls? $\endgroup$ – trexxet Aug 30 '18 at 13:36
  • 1
    $\begingroup$ @trexxet: I don't know what you mean. I said "Everything else is perfectly valid." $\endgroup$ – Nicol Bolas Aug 30 '18 at 13:51
  • $\begingroup$ 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. $\endgroup$ – trexxet Aug 30 '18 at 18:01
  • $\begingroup$ @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. $\endgroup$ – Nicol Bolas Aug 30 '18 at 18:02
  • $\begingroup$ 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? $\endgroup$ – trexxet Aug 30 '18 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.