Search code examples
openglglslshaderopengl-4

GLSL Uniform layout binding and textures


I'm a bit confused on what would be the right way to bind the texture when uniforms are using the layout binding.

layout(binding = 0, std140) uniform uCommon
{
    mat4 projectionMatrix;
    mat4 viewMatrix;
};

layout(binding = 1, std140) uniform uModel
{
    mat4 modelViewProjectionMatrix;
};

layout(binding = 3) uniform sampler2D uTexture;

To bind my first texture I should use "GL_TEXTURE0 + 3"?

glActiveTexture(GL_TEXTURE0 + 3);
glBindTexture(GL_TEXTURE_2D, textureId);

Is this the correct way?

EDIT: Or is sampler using a separate binding from other uniforms? Can I use:

layout(binding = 0) uniform sampler2D uTexture;

while still using

layout(binding = 0, std140) uniform uCommon

Solution

  • Uniform block binding indices have nothing to do with sampler binding locations. These are different things.

    The integer-constant-expression, which is used to specify the binding point or unit has not to be unique across all usages of the keyword binding.


    See OpenGL Shading Language 4.60 Specification; 4.4.5 Uniform and Shader Storage Block Layout Qualifiers; page 77

    The binding identifier specifies the uniform buffer binding point corresponding to the uniform or shader storage block, which will be used to obtain the values of the member variables of the block.


    See OpenGL Shading Language 4.60 Specification; 4.4.6 Opaque-Uniform Layout Qualifiers; page 79

    Image and sampler types both take the uniform layout qualifier identifier for binding:

    layout-qualifier-id :
        binding = integer-constant-expression
    

    The identifier binding specifies which unit will be bound.