Understanding when to call glBindBufferBase

Viewed 369

I have some SSBO to be accessed in the shader. Lets say its something like

layout(std430, binding = 2) readonly buffer MyBuffer
{
    int myData[];
};

When is the appropriate time to call glBindBufferBase?

Right now I do something like

glBindBuffer(GL_SHADER_STORAGE_BUFFER, name)
glBufferData(...)
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, name)
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0)

I do not know if this sets some global state whereby index 2 is set for every single shader I bind and unbind.

Can you tell me if there's any flaws with this idea:

Create, Bind SSBO, upload data, unbind for the SSBO

Bind shader
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, name)
Draw arrays
Unbind shader

Is this the expected way to use it? I only bind the SSBO once, upload, and then I never bind it again. This seems to work but I have no idea if it "just works" because my GPU tries to be smart for me, or if I'm violating the spec (which I went through but couldn't find the answer to this question). I've been burned before where I do something that will work on my GPU and then fail on another one, and that makes me hesitant of what I'm doing.

Ideally I'd like to make a single function call to swap in my SSBO into some binding, and then draw. I'm unsure if I can do this before binding the shader, or if the shader has to be bound. If there's a better way to do this, please advise.

In short, the end goal is the minimal way to get some SSBO bound on some index after it is ready to be used from uploading data.

1 Answers

OpenGL is a state engine. Once a status has been set, it is retained until it is changed again(, even beyond frames).
The buffer object stays bound to the indexed buffer target as long as you do not bind another buffer object to the same binding point. You only need to call glBindBuffer, because operations glBufferData and glBindBufferBase use the currently bound buffer for there operations.

Related