1

I've been delving into OpenGL a while, and now, working on a project, I found that, when I'm creating an index buffer, if I bind it to a GL_ARRAY_BUFFER instead to a GL_ELEMENT_ARRAY_BUFFER has (apparently) the same result.

I mean, the vertex buffers are always bound to GL_ARRAY_BUFFER, but if I create an index buffers like this:

glCreateBuffers(1, &m_BufferID);
glBindBuffer(GL_ARRAY_BUFFER, m_BufferID);
glBufferData(GL_ARRAY_BUFFER, count * sizeof(uint), vertices, GL_STATIC_DRAW);

And then, when drawing geometry for instance, I bind it to a GL_ELEMENT_ARRAY_BUFFER, works just fine, and I don't know why I thought that index buffers had to be created with GL_ELEMENT_ARRAY_BUFFER too, but... Is there any "inner" difference actually?

LuchoSuaya
  • 77
  • 10
  • 2
    If you're using `glCreateBuffers`, then you're using DSA, so you shouldn't be calling `glBindBuffer` *ever*. – Nicol Bolas Nov 11 '20 at 16:47
  • `GL_ARRAY_BUFFER` for indexes might work on some machines and not on others ... at least in past ATI cards and latter AMD throw exceptions on certain circumstances if `GL_ELEMENT_ARRAY_BUFFER` was not used and or the datatype was not a specific type and or the number of indexes crosses some threshold (IIRC around 37 or 57 KByte) – Spektre Nov 12 '20 at 08:26

2 Answers2

2

In terms of the nature of the buffer object itself? No. All buffer objects are the same and can be used for any task appropriate for a buffer object. A buffer object does not take on special properties by which binding it was initially used with.

However, the GL_ELEMENT_ARRAY_BUFFER binding point is itself a bit unusual. It's not part of global context state; it's part of VAO state. So if you have no VAO bound (under a core profile context), then you can't bind anything to that binding point. And when you bind to that binding point, you are affecting the state of the currently bound VAO. And if you change the currently bound VAO, you will be changing which buffer is bound to the element array binding point.

So generally, you should only bind to that point if what you intend to do is attach the buffer to the currently bound VAO.

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

Yes it ist. While the ARRAY_BUFFER binding is a global state, the ELEMENT_ARRAY_BUFFER binding is stated in the Vertex Array Object. See Index buffers.
Hence, glBindBuffer(GL_ELEMENT_ARRAY_BUFFER m_BufferID) changes a state in the currently bound Vertex Array Object.

Note that compared to the index buffer (ELEMENT_ARRAY_BUFFER), the vertex buffer binding (ARRAY_BUFFER) is a global state.
Each attribute which is stated in the VAOs state vector may refer to a different ARRAY_BUFFER. This reference is stored when glVertexAttribPointer is called. Then the buffer which is currently bound to the target ARRAY_BUFFER, is associated to the specified attribute index and the name (value) of the object is stored in the state vector of the currently bound VAO.
However the index buffer is a state of the VAO. If a buffer is bound to the target ELEMENT_ARRAY_BUFFER, this buffer is assigned to the currently bound Vertex Array Object.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174