c++openglopengl-4direct-state-access

How to create a SSBO with dynamic size


I have looked everywhere but couldn't find one that used DSA (Direct State Access). I need a SSBO with dynamic size so that I can update my instances WITHOUT having a limit like MAX_INSTANCE_COUNT. At the moment what I do is:

size_t datasize = 0;
void* data = nullptr;
unsigned int index = 0;

unsigned int id = 0;
glCreateBuffers(1, &id);
glNamedBufferStorage(id, datasize, data, GL_DYNAMIC_STORAGE_BIT);
glBindBufferRange(GL_SHADER_STORAGE_BUFFER, index, id, 0, datasize);

Then in the loop what I do is:

glNamedBufferData(id, (GLsizeiptr)datasize, data, GL_STREAM_DRAW);

I did use "glNamedBufferSubData" but that doesn't really let me go above the datasize. If you could explain step by step how I could do this, I'd really appreciate it.


Solution

  • OpenGL doesn't have a concept of "dynamic storage". You can use BufferData to allocate new storage for them, but you really shouldn't. It's best to preallocate storage and if you blow past that limit, either generate an error or allocate a new buffer entirely.

    Also, you cannot call BufferData on a buffer with immutable storage (which your call to BufferStorage created). The whole point of immutable storage buffers is that you can't reallocate them.