<p>While OpenGL's organic growth as an API has led to some inconsistencies regarding the naming of things, the API here means what it says. <code>location</code> means <em>location</em>, and <code>binding</code> means binding. As such, a function named <code>glGetUniformLocation</code> should be expected to return the <code>location</code> of the uniform, not the <code>binding</code>.</p>
<p>When introspecting GLSL, there are several quantities to consider. For a particular kind of thing (program inputs, uniforms, etc. In the <a href="https://www.khronos.org/opengl/wiki/Program_Introspection#Interfaces" rel="nofollow noreferrer">modern introspection API, these are called &quot;interfaces&quot;</a>), all of the introspectable variables (<a href="https://www.khronos.org/opengl/wiki/Program_Introspection#Interface_query" rel="nofollow noreferrer">called &quot;resources&quot;</a>) are given an index as if they're all in an array. This index is from 0 to the number of active resources in this interface.</p>
<p>You can then <a href="https://www.khronos.org/opengl/wiki/Program_Introspection#Resource_properties" rel="nofollow noreferrer">query properties about a resource (name, etc)</a> by its index. But the index of a resource is neither its <code>location</code> nor its <code>binding</code>; it is merely an identifier for the resource.</p>
<p>Some resources have a location; a number used by the API to directly affect the value it gets or gives. Program inputs use locations to determine which variable gets when data from the VAO (or prior program stage if the program doesn't contain a VS). Uniform locations are how you set values into the uniform via <code>glProgramUniform*</code> calls. And so forth.</p>
<p>Some resources have a binding; a number which represents the index in a table (in the OpenGL context) for how the resource is used to access memory of some kind. The binding for opaque types determine which binding slot the given object represents. To make a memory object (texture or buffer) available to this resource, you bind that object to the binding index for that kind of access (image load/store, texture, etc).</p>
<p>So if you have an image uniform, the <code>location</code> determines how you would set its binding from the API via <code>glProgramUniform1i</code>. But the <code>binding</code> determines which index in the OpenGL context the OpenGL object you're trying to access will be bound to. If you do not assign a location to a uniform in the shader, the API will assign one itself, which you would have to query (if you need to change the binding for some reason. Which you shouldn't).</p>

CC BY-SA 4.0
2022-01-13T15:10:23.330Z
2022-01-13T15:10:23.330Z
Nicol Bolas
<p><a href="https://www.khronos.org/opengl/wiki/Interface_Block_(GLSL)" rel="nofollow noreferrer">https://www.khronos.org/opengl/wiki/Interface_Block_(GLSL)</a></p>
<p>uniform without buffer backed, only location, no binding.</p>
<p>location similar to position offset.</p>
<pre><code>// shader code
layout (location = 0) uniform float fTime;

// host code
location = 0;
glUniform1f(location, 45.2f);  // update data to uniform location 0
</code></pre>
<p>more shader example</p>
<pre><code>// each array element consume 1 location
layout(location = 0) out vec3 vals[4];      // Consumes 4 locations
// each struct field consume location
struct OutData{
    vec3 data1;                             // Consumes 1 location
    dvec4 data2;                            // Consumes 2 location
    float val[3];                           // Consumes 3 location
};
layout(location = 4) out OutData myOut;     // Consumes 6 locations
layout(location = 10) out vec2 texCoord;    // Consumes 1 location
</code></pre>
<p>uniform block with buffer backed, only binding, no location.</p>
<p>binding is something like block index, the field offset inside the buffer similar to location.</p>
<pre><code>// shader code
layout(binding = 0, std140) uniform MainBlock { // blockIndex = 0
    vec3 data;
};

// host code
storageBlockIndex= 0;
uniformBlockBinding = 1; // any value
glUniformBlockBinding(program, storageBlockIndex, uniformBlockBinding);
glBindBufferBase(GL_UNIFORM_BUFFER, uniformBlockBinding, bufferId);
</code></pre>
<p>relation of block index and binding point:</p>
<pre><code>buffer id          binding points 0            uniform block index
buffer id   \/     binding points 1    \/      uniform block index
buffer id   /\     binding points 2    /\      uniform block index
</code></pre>
<p>for sampler, binding is texture unit, also can specify location</p>
<pre><code>// shader code
//layout (binding = 0) uniform sampler2D foo;
layout (location = 0, binding = 1) uniform sampler2D foo;

// host code
location = 0;
unit = 1;
glActiveTexture(GL_TEXTURE0 + unit);      // set active unit
glBindTexture(GL_TEXTURE_2D, textureId);  // put to current active unit

glUniformli(location, unit);
</code></pre>
<p>relation of texture unit and uniform location:</p>
<pre><code>texture id          texture unit 0            uniform location
texture id   \/     texture unit 1    \/      uniform location
texture id   /\     texture unit 2    /\      uniform location

 
</code></pre>

CC BY-SA 4.0
2023-11-17T09:13:10.320Z
2023-11-17T09:49:11.797Z
2023-11-17T09:49:11.797Z
andrewchan2022
<p>The background of this question is that I am trying to write an automatic binding system, where for each uniform variable I want to declare in my GLSL code, I call a method <code>declareUniform</code> which is counting up the location value, saving the type and name of the uniform to a list which will be saved within my shaderobject...</p>
<p>I already did something similar with varyings, where I save the varying information (location, type, name, shader stage from, shader stage to, isarray, arraycount) and of cause also generating glsl string which is working fine.</p>
<p>I am trying to bind several things to a shader, which does not work right now.
I've read, that uniform block binding indices have nothing to do with sampler binding locations <a href="https://stackoverflow.com/questions/49097947/glsl-uniform-layout-binding-and-textures">here</a>, which is totally confusing me... I also read the <code>Explicit_uniform_location</code> site from khronos <a href="https://www.khronos.org/opengl/wiki/Layout_Qualifier_(GLSL)#Explicit_uniform_location" rel="nofollow noreferrer">here</a>.
I am trying to bind some sampler2D, image2D and other uniforms in one shader.</p>
<pre><code>layout(location = 0) in  vec2 texCoordIn; //value from VBO
layout(binding = 0, rgba32f) uniform  image2D writeColorTexture;
layout(binding = 1) uniform  sampler2D readColorTexture;
layout(location = 0) uniform  float u_apature;
layout(location = 1) uniform  float u_focalDistance;
...
</code></pre>
<p>On CPU it looks like this:</p>
<pre><code>GLint readColorTextureLoc = glGetUniformLocation(glShaderHandle, &quot;readColorTexture&quot;);
GLint writeColorTextureLoc = glGetUniformLocation(glShaderHandle, &quot;writeColorTexture&quot;);
</code></pre>
<p>trying to bind the samper2D:</p>
<pre><code>glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glUniform1i(readColorTextureLoc, 0);
glBindTexture(GL_TEXTURE_2D, colorTexture-&gt;id());
</code></pre>
<p>trying to bind the image2D:</p>
<pre><code>glBindImageTexture(writeColorTextureLoc, colorImage-&gt;id(), 0, GL_TRUE, 0, GL_WRITE_ONLY, GL_RGBA32F);
</code></pre>
<p>when using <code>glGetUniformLocation(glShaderHandle, &quot;readColorTexture&quot;);</code> I get the value 31 which also confuses me.
This brought me to understand it to the following way: <code>layout(location = ...) uniform</code> is the value which will be returned by <code>glGetUniformLocation</code>.</p>
<p>The binding qualifier maps the uniform variable to a (I'll call it) &quot;background list&quot;, which can be addressed by <code>glActiveTexture</code> for example if it is a sampler. As far as I understood (please correct me if I am wrong!) there are several of these &quot;background lists&quot;. One for samplers, one for block bindings, one for images... and each of them starts with 0.</p>
<p>Now my question: Am I right? Did I understand it correctly? If so: which &quot;background lists&quot; are available and how can I bind a sampler together with an Image?</p>

CC BY-SA 4.0
2022-01-13T09:35:25.297Z
I don't want this example to just work... I want it to be perfect, so when adding another shader, that no effects start to appear, which needs a lot of time to debug. So it need to be a closed concept, which fully controls each binding slot
Thomas
CC BY-SA 4.0
2022-01-13T14:54:14.087Z
"*automatic binding system*" Don't do that. If your shader has hard-coded into it an image binding point, just *use that*. There's no need to query what you already know is there.
Nicol Bolas
<opengl><glsl>
Opengl GLSL binding locations confusion
Thomas
# Opengl GLSL binding locations confusion

(Asked by: username_0 on 2022-01-13)

The background of this question is that I am trying to write an automatic binding system, where for each uniform variable I want to declare in my GLSL code, I call a method `declareUniform` which is counting up the location value, saving the type and name of the uniform to a list which will be saved within my shaderobject...

I already did something similar with varyings, where I save the varying information (location, type, name, shader stage from, shader stage to, isarray, arraycount) and of cause also generating glsl string which is working fine.

I am trying to bind several things to a shader, which does not work right now.
I've read, that uniform block binding indices have nothing to do with sampler binding locations [here](https://stackoverflow.com/questions/49097947/glsl-uniform-layout-binding-and-textures), which is totally confusing me... I also read the `Explicit_uniform_location` site from khronos [here](https://www.khronos.org/opengl/wiki/Layout_Qualifier_(GLSL)#Explicit_uniform_location).
I am trying to bind some sampler2D, image2D and other uniforms in one shader.

```
layout(location = 0) in  vec2 texCoordIn; //value from VBO
layout(binding = 0, rgba32f) uniform  image2D writeColorTexture;
layout(binding = 1) uniform  sampler2D readColorTexture;
layout(location = 0) uniform  float u_apature;
layout(location = 1) uniform  float u_focalDistance;
...

```

On CPU it looks like this:

```
GLint readColorTextureLoc = glGetUniformLocation(glShaderHandle, "readColorTexture");
GLint writeColorTextureLoc = glGetUniformLocation(glShaderHandle, "writeColorTexture");

```

trying to bind the samper2D:

```
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glUniform1i(readColorTextureLoc, 0);
glBindTexture(GL_TEXTURE_2D, colorTexture->id());

```

trying to bind the image2D:

```
glBindImageTexture(writeColorTextureLoc, colorImage->id(), 0, GL_TRUE, 0, GL_WRITE_ONLY, GL_RGBA32F);

```

when using `glGetUniformLocation(glShaderHandle, "readColorTexture");` I get the value 31 which also confuses me.
This brought me to understand it to the following way: `layout(location = ...) uniform` is the value which will be returned by `glGetUniformLocation`.

The binding qualifier maps the uniform variable to a (I'll call it) "background list", which can be addressed by `glActiveTexture` for example if it is a sampler. As far as I understood (please correct me if I am wrong!) there are several of these "background lists". One for samplers, one for block bindings, one for images... and each of them starts with 0.

Now my question: Am I right? Did I understand it correctly? If so: which "background lists" are available and how can I bind a sampler together with an Image?

**Tags:** opengl, glsl

> **Comments:**
> **username_0**: I don't want this example to just work... I want it to be perfect, so when adding another shader, that no effects start to appear, which needs a lot of time to debug. So it need to be a closed concept, which fully controls each binding slot  
> **username_1**: "*automatic binding system*" Don't do that. If your shader has hard-coded into it an image binding point, just *use that*. There's no need to query what you already know is there.  

---

## 2 answers

### Answer by username_1 on 2022-01-13. Score: 3 | Accepted answer

While OpenGL's organic growth as an API has led to some inconsistencies regarding the naming of things, the API here means what it says. `location` means *location*, and `binding` means binding. As such, a function named `glGetUniformLocation` should be expected to return the `location` of the uniform, not the `binding`.

When introspecting GLSL, there are several quantities to consider. For a particular kind of thing (program inputs, uniforms, etc. In the [modern introspection API, these are called "interfaces"](https://www.khronos.org/opengl/wiki/Program_Introspection#Interfaces)), all of the introspectable variables ([called "resources"](https://www.khronos.org/opengl/wiki/Program_Introspection#Interface_query)) are given an index as if they're all in an array. This index is from 0 to the number of active resources in this interface.

You can then [query properties about a resource (name, etc)](https://www.khronos.org/opengl/wiki/Program_Introspection#Resource_properties) by its index. But the index of a resource is neither its `location` nor its `binding`; it is merely an identifier for the resource.

Some resources have a location; a number used by the API to directly affect the value it gets or gives. Program inputs use locations to determine which variable gets when data from the VAO (or prior program stage if the program doesn't contain a VS). Uniform locations are how you set values into the uniform via `glProgramUniform*` calls. And so forth.

Some resources have a binding; a number which represents the index in a table (in the OpenGL context) for how the resource is used to access memory of some kind. The binding for opaque types determine which binding slot the given object represents. To make a memory object (texture or buffer) available to this resource, you bind that object to the binding index for that kind of access (image load/store, texture, etc).

So if you have an image uniform, the `location` determines how you would set its binding from the API via `glProgramUniform1i`. But the `binding` determines which index in the OpenGL context the OpenGL object you're trying to access will be bound to. If you do not assign a location to a uniform in the shader, the API will assign one itself, which you would have to query (if you need to change the binding for some reason. Which you shouldn't).


---

### Answer by username_2 on 2023-11-17. Score: -1

<https://www.khronos.org/opengl/wiki/Interface_Block_(GLSL)>

uniform without buffer backed, only location, no binding.

location similar to position offset.

```
// shader code
layout (location = 0) uniform float fTime;

// host code
location = 0;
glUniform1f(location, 45.2f);  // update data to uniform location 0

```

more shader example

```
// each array element consume 1 location
layout(location = 0) out vec3 vals[4];      // Consumes 4 locations
// each struct field consume location
struct OutData{
    vec3 data1;                             // Consumes 1 location
    dvec4 data2;                            // Consumes 2 location
    float val[3];                           // Consumes 3 location
};
layout(location = 4) out OutData myOut;     // Consumes 6 locations
layout(location = 10) out vec2 texCoord;    // Consumes 1 location

```

uniform block with buffer backed, only binding, no location.

binding is something like block index, the field offset inside the buffer similar to location.

```
// shader code
layout(binding = 0, std140) uniform MainBlock { // blockIndex = 0
    vec3 data;
};

// host code
storageBlockIndex= 0;
uniformBlockBinding = 1; // any value
glUniformBlockBinding(program, storageBlockIndex, uniformBlockBinding);
glBindBufferBase(GL_UNIFORM_BUFFER, uniformBlockBinding, bufferId);

```

relation of block index and binding point:

```
buffer id          binding points 0            uniform block index
buffer id   \/     binding points 1    \/      uniform block index
buffer id   /\     binding points 2    /\      uniform block index

```

for sampler, binding is texture unit, also can specify location

```
// shader code
//layout (binding = 0) uniform sampler2D foo;
layout (location = 0, binding = 1) uniform sampler2D foo;

// host code
location = 0;
unit = 1;
glActiveTexture(GL_TEXTURE0 + unit);      // set active unit
glBindTexture(GL_TEXTURE_2D, textureId);  // put to current active unit

glUniformli(location, unit);

```

relation of texture unit and uniform location:

```
texture id          texture unit 0            uniform location
texture id   \/     texture unit 1    \/      uniform location
texture id   /\     texture unit 2    /\      uniform location

 

```


---

