Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL Uniform Buffer confusion

Tags:

opengl

Can someone tell me about the seemingly needlessly complicated Uniform Buffers? I've read the section in OpenGL Superbible 5, I've looked at some examples on blogs and I've read the official spec, and I still don't get it.

Specifically, all examples seem to require a shader program to begin with in order to setup the uniform buffer initially with glGetActiveUniformsiv. I don't understand this. Why doesn't the interface allow you to define the structure without referring to a shader program, validating the buffer/s formats against the program at link time?

Secondly, if I get the structure layout from one program, assuming the structure layout is the same for all programs that use the set of uniforms, is the structure guaranteed to have the same offsets, data sizes and so on? I would assume so.

Thirdly, I don't understand binding points. I have to call glBindBufferBase with an index, and then call glUniformBlockBinding with a block index and the index I passed into glBindBufferBase. I'm having trouble visualising exactly what's going on here. The Superbible lacks clarity, as does the spec and the samples I've seen.

like image 480
Robinson Avatar asked Sep 09 '11 20:09

Robinson


1 Answers

  1. Why exactly would you want to? Personally, unless you have a serious data shortage problem and need every byte, I see no reason to use anything other than std140. It makes the code so much cleaner.

    However, if you're dead-set on avoiding std140, read on.

  2. The specification thoroughly explained this: it's all in the layout qualifiers.

    There are exactly 3 layout qualifiers: packed, shared, and std140. packed means that the implementation is free to arrange everything pretty much as it wants. This includes removing uniforms that the current program does not use. So the layout is implementation-defined, and certain uniforms within that layout may have been optimized away.

    shared means that the implementation is free to arrange the data just like packed. However, it must provide storage for each of the uniforms. This makes it possible to share uniform layout among programs, hence the name. shared also requires that the implementation will provide a consistent layout for consistent definitions across programs. Therefore, you only need to do the querying for one layout.

    To answer your first question, you could if you so desire create a fake program with shared layout. You could use it just to query the layout for the uniform block. Then, as long as the layout is consistent across other programs (with the shared layout), the layouts will all be the same. So there's no need for a special API for it.

    std140 however means that the layout of a uniform block is defined explicitly by the OpenGL implementation, byte for byte. This implicitly allows sharing, since two identical uniform blocks under this specification will have the identical layout. And since the implementation cannot optimize away uniforms in a std140 layout block, everything is perfect.

    Again, there is almost no reason to avoid std140. Not unless you are under very, very severe memory constraints.

  3. It's the exact same mechanism as for textures. The only difference is that uniform block names are not themselves uniforms.

    Texture objects are bound to texture image units, using glActiveTexture(GL_TEXTURE0 + i);glBindTexture(), where i is the texture image unit index. You now need to tell the shader which sampler uses that image unit. However, OpenGL doesn't let you do the association directly with the name; you have to convert the sampler name into an index location. So you get the uniform location to a particular sampler with glGetUniformLocation. Once you have the uniform location, you can associate the texture image unit with that location by calling glUniform1i(loc, i), where again i is the texture image unit you bound the texture to.

    Uniform buffer objects are bound to uniform buffer binding points, using glBindBufferRange(GL_UNIFORM_BUFFER, i, ...), where i is the uniform buffer binding point. You now need to tell the shader which uniform block uses that binding point. However, OpenGL doesn't let you do the association directly with the name; you have to convert the uniform block into an index location. So you get the index to a particular uniform block with glGetUniformBlockIndex. Once you have the index, you can associate the uniform buffer binding point with that index by calling glUniformBlockBinding(program, index, i), where again i is the uniform buffer binding points you bound the uniform buffer to.

    See? Exactly the same. They use different terms, but structurally, they're the same. If you need a picture, then you can find a more thorough discussion that includes a diagram here.

like image 109
Nicol Bolas Avatar answered Oct 18 '22 12:10

Nicol Bolas