GL_SHADER_STORAGE_BUFFER and GL_ELEMENT_ARRAY_BUFFER on same Buffer object?

Hi, I am new to these forums and I have a question for you advanced OpenGL coders :slight_smile:

I want a Compute Shader to write the indices of my mesh. So, I need to create an index buffer (GL_ELEMENT_ARRAY_BUFFER) which should also be randomly writable from my Compute Shader (so I need it to be a GL_SHADER_STORAGE_BUFFER).

Speaking in D3D11 terms, I want to mimic this functionality from D3D11 which is able to create one single buffer that can be used in both ways as follows:

bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER | D3D11_BIND_UNORDERED_ACCESS;

The problem is: I don’t know if that’s possible with OpenGL and how to do that.

I think I could create 1 buffer name with glGenBuffers() and then bind the two different targets to it, but I couldn’t find any example or hint anywhere that this could be possible

I know this could be done by writing my indices into a GL_SHADER_STORAGE_BUFFER object and then copying it to a GL_ELEMENT_ARRAY_BUFFER, but I want to avoid copeis as much as possible.

Any ideas?

Thanks for your time.

Buffer objects can be bound to any target at any time. The specification says that “the GL may make different choices about storage location and layout based on the initial binding”, but that doesn’t happen in practice.

So to generate an index buffer from a compute shader, you’d create the buffer and its storage, bind it to a SSBO binding point with e.g. glBindBufferBase() or glBindBufferRange() with GL_SHADER_STORAGE_BUFFER as the target, execute the compute shader to update the buffer, bind it as an index buffer with glBindBuffer(GL_ELEMENT_ARRAY_BUFFER), then execute the draw call(s).