Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's glUniformBlockBinding used for?

Tags:

opengl

Assuming I have a shader program with a UniformBlock at index 0.

Binding the UniformBuffer the following is apparently enough to bind a UniformBuffer to the block:

glUseProgram(program);
glBindBuffer(GL_UNIFORM_BUFFER, buffer);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, buffer);

I only have to use glUniformBlockBinding when I bind the buffer to a different index than used in the shader program.

//...
glBindBufferBase(GL_UNIFORM_BUFFER, 1, buffer)
glUniformBlockBinding(program, 0, 1); // bind uniform block 1 to index 0

Did I understand it right? Would I only have to use glUniformBlockBinding if I use use the buffer in different programs where the appropriate blocks have different indices?

like image 342
Appleshell Avatar asked Apr 21 '14 18:04

Appleshell


1 Answers

Per-program active uniform block indices differ from global binding locations.

The general idea here is that assuming you use the proper layout, you can bind a uniform buffer to one location in GL and use it in multiple GLSL programs. But the mapping between each program's individual buffer block indices and GL's global binding points needs to be established by this command.

To put this in perspective, consider sampler uniforms.

Samplers have a uniform location the same as any other uniform, but that location actually says nothing about the texture image unit the sampler uses. You still bind your textures to GL_TEXTURE7 for instance instead of the location of the sampler uniform.

The only conceptual difference between samplers and uniform buffers in this respect is that you do not assign the binding location using glUniform1i (...) to set the index. There is a special command that does this for uniform buffers.


Beginning with GLSL 4.20 (and applied retroactively by GL_ARB_shading_language_420pack), you can also establish a uniform block's binding location explicitly from within the shader.

GLSL 4.20 (or the appropriate extension) allows you to write the following:

layout (std140, binding = 0) uniform MyUniformBlock
{
  vec4 foo;
  vec4 bar;
};

Done this way, you never have to determine the uniform block index for MyUniformBlock; this block will be bound to 0 at link-time.

like image 178
Andon M. Coleman Avatar answered Oct 05 '22 18:10

Andon M. Coleman