PDA

View Full Version : Modifying buffer after calling glVertexAttribPointer()



gcs128
02-13-2016, 04:24 PM
I'm using OpenGL ES 2.0 on iOS in Swift. The posting guidelines mention that the focus here is mostly on desktop OpenGL, but that ES questions are still welcome. Although I could be wrong, I think this question isn't specific to ES.

Typically, the order of operations when setting up for drawing seems to be something like this (I'm not using VAOs):


glBindBuffer()
glBufferData()/glBufferSubData()
glEnableVertexAttribArray()
glVertexAttribPointer()
glDrawElements()

I'm wondering though if there's anything wrong with creating the buffer store and/or modifying buffer contents after setting up the vertex attributes, e.g.:


glBindBuffer()
glEnableVertexAttribArray()
glVertexAttribPointer()
glBufferData()/glBufferSubData()
glDrawElements()

An example case where this might come up in practice would be a quad batching system. In such a system you might set up the vertex attribute state once at the beginning:


glBindBuffer()
glBufferData()
glEnableVertexAttribArray()
glVertexAttribPointer()

Then, for each batch:


glBufferSubData()
glDrawElements()

Here, you'd be calling glBufferSubData() (repeatedly) after having called glVertexAttribPointer().

The spec says that glVertexAttribPointer() stores the buffer ID only (which should be independent of the storage location and contents), so it doesn't seem like calling glBufferData() or glBufferSubData() subsequent to that would cause any problems. And, I'm not seeing any problems with it in practice. Of course, just because something happens to work in practice doesn't necessarily mean it'll always work.

Is calling glBufferData() or glBufferSubData() on a buffer that's currently part of vertex attribute state safe to do? Or is there some reason it shouldn't be done that I'm missing?

Dark Photon
02-13-2016, 04:55 PM
I'm wondering though if there's anything wrong with creating the buffer store and/or modifying buffer contents after setting up the vertex attributes, e.g.:

That will work. However, for best performance, you do need to be careful to upload data to the driver in a manor that pipelines well and avoids CPU-GPU synchronizations. On that note, this is a good web page to read to prime your thinking:

* Buffer Object Streaming (OpenGL wiki) (https://www.opengl.org/wiki/Buffer_Object_Streaming)

There are also some good slide presentations that describe some of this. Let me know if you're interested, and I'll dig up URLs.

gcs128
02-13-2016, 05:14 PM
Thanks, Dark Photon. Yeah, I'm aware of the potential performance issues, but I'm mostly worried about correctness for the moment. (If performance becomes an issue I'll look into other options such as buffer mapping or using multiple buffers.)

Thanks for your help.