Search code examples
c++openglshaderopengl-4

Binding a single buffer to multiple indexed targets of the SSBO, simultaneously


Am I allowed to bind a single opengl buffer to multiple indexed targets (of the SSBO target) simultaneously?

For instance, suppose my shader has two different uniform blocks with different binding indexes. If the information I need is located in the same buffer, am I allowed to use glBindBufferRange, and bind different ranges of the same buffer to these two binding indexes, simultaneously?

Another use case I see is, for instance, if I have a shader with two uniform blocks, again with different binding indexes, but this time, the only data member both uniform blocks have is an open array (with unspecified size). Am I allowed to use glBindBuffer to bind the same buffer to both uniform blocks, and guarantee by code, to only access the array indexes within the proper range in the buffer?


Solution

  • I believe it's fine to do so.

    §6.1 (...) While a buffer object is bound, GL operations on the target to which it is bound affect the bound buffer object, and queries of the target to which a buffer object is bound return state from the bound object. Operations on the target also affect any other bindings of that object

    emphasis mine - which would directly suggest it's OK.

    §6.1.1. (...) Each target represents an indexed array of buffer object binding points, as well as a single general binding point that can be used by other buffer object manipulation functions, such as BindBuffer or MapBuffer. Both commands bind the buffer object named by buffer to both the general binding point, and to the binding point in the array given by index. If the binds are successful no change is made to the state of the bound buffer object, and any previous bindings to the general binding point or to the binding point in the array are broken

    What I'd distill from that is that it's not explicitely forbidden to bind a buffer range to multiple places, and as such, I'd assume it's allowed. It won't break the other bindings in that array, which means the previously bound ranges should stay unchanged and valid.

    That being said, if the ranges overlap and you're writing to them, you might need barriers.