4

I'm learning about OpenGL/ES. I'm not clear what a binding point is. I looked at the OpenGL documents and also searched the Internet, but couldn't find something which explains binding point well.

Here is an example of what I mean by binding point.

void glUniformBlockBinding( GLuint program, GLuint uniformBlockIndex, GLuint uniformBlockBinding);

I would appreciate it if you can explain what a binding point is, how it is used, etc.

genpfault
  • 51,148
  • 11
  • 85
  • 139
shervin0
  • 41
  • 2

1 Answers1

3

Uniform block binding points act in exactly the same way as texture units.

In your shader, you refer to a texture sampler (e.g. a sampler2D) and you associate this sampler to a texture unit by calling glUniform1i(samplerUniformIndex, textureUnitNumber);. Before drawing with the shader, you then bind a texture to this textureUnitNumber using a combination of glActiveTexture (to set the active texture unit), and glBindTexture to bind a texture to the active texture unit. So there are three concepts here:

  1. A binding point (the texture unit)
  2. The resource to be used (the texture)
  3. The thing using the resource (the sampler uniform in your shader)

Binding points for uniform blocks are the same. You have:

  1. A uniform block binding point
  2. A resource to be used. In this case this is the buffer object which stores the uniform block data.
  3. The thing using the resource, which is the uniform block in your shader.

We can compare how a texture and uniform buffer objects are used.

Associating the binding point with the thing using the resource:

Textures:

GLuint samplerUniformIndex;
glUseProgram(program);
samplerUniformIndex = glGetUniformLocation(program, "mysampler");
glUniform1i(samplerUniformIndex, textureUnitNumber);

Uniform buffers:

GLuint uniformBlockIndex = glGetUniformBlockIndex(program, "myblock");
glUniformBlockBinding(program, uniformBlockIndex, uniformBlockBinding);

Associating the resource with its binding point:

Textures:

glActiveTexture(textureUnitNumber);
glBindTexture(textureTarget, texture);

Uniform buffers:

glBindBufferBase(GL_UNIFORM_BUFFER, uniformBlockBinding, uniformBuffer);

Or you can use glBindBufferRange to bind a specific range of the uniform buffer.

Michael IV
  • 11,016
  • 12
  • 92
  • 223
Alex Lo
  • 326
  • 2
  • 4
  • 2
    Instead of "glUniformBlockIndex(program, uniformBlockIndex, uniformBlockBinding);" did you mean "glUniformBlockBinding(program, uniformBlockIndex, uniformBlockBinding);"? – Steven2163712 Sep 01 '18 at 12:07