same buffer bound to different targets?


Can you change the target of a buffer?

For example:

glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(float), myData,GL_READ_ONLY);

I later want the same buf to be bound to a GL_SHADER_STORAGE_BUFFER target. Is this even possible?


If not, what’s the best way to accomplish this? I do not want to create two different buffer objects, store the same myData in them and bind them to the two different GL_ARRAY_BUFFER and GL_SHADER_STORAGE_BUFFER targets. The reason is that myData stored in buf gets modified and is made resident on gpu memory. The next target, GL_SHADER_STORAGE_BUFFER, should see the modified memory.


The specification states that an implementation may optimise storage according to the first target to which the buffer is bound, so you shouldn’t “recycle” buffers simply to avoid deleting and creating buffers.

But it’s perfectly legal (and quite common) to bind a buffer to one target to write into it then bind it to a different target to read from it. E.g. copying pixel data by first binding to GL_PIXEL_PACK_BUFFER then later to GL_PIXEL_UNPACK_BUFFER. Or binding to GL_TRANSFORM_FEEDBACK_BUFFER to capture the output from a vertex shader then later binding to GL_ARRAY_BUFFER to use the captured data.

In the case where you’re reading one set of data using multiple interfaces, it may be more efficient to use a separate buffer for each type of access, but it’s not guaranteed that’s the case, and even if there is a benefit it may not be sufficient to outweigh the cost of using multiple buffers.