<p>There is no such thing as an &quot;in image&quot; and an &quot;out image&quot;. There are just <a href="https://www.khronos.org/opengl/wiki/Image_Load_Store" rel="nofollow noreferrer">images used for load/store operations</a>. A particular image can be write-only or read-only, but what &quot;a particular image&quot; <em>means</em> is defined by the binding index used in the shader. That is, there is exactly and only one image bound to index 1 at any time, with exactly and only one set of properties (readable, writeable, etc). If you bind another image to index 1, then you have <em>unbound</em> the previous image, and you have changed the properties of the binding.</p>
<p>Whatever <code>p4ImIn</code> and <code>p4ImOut</code> represent in your shader, they should have different binding indices. And therefore, you should be binding those images to different indices.</p>

CC BY-SA 4.0
2023-01-31T23:53:31.623Z
Thank you Nicol. Of course you are right. Sorry for the messy code. I just have the convention of naming targets for writing or store operations for "out" and targets for reading or load for "in". I realized just 1 hour ago I had mixed up image units and texture units.
mathreadler
CC BY-SA 4.0
2023-01-31T22:49:31.423Z
2023-01-31T22:49:31.423Z
Nicol Bolas
<p>I am new at this particular site in the SE ecosystem, but not to computer graphics in general. If I'm in the wrong spot feel free to redirect me.</p>
<p>My question regards glsl programming and binding two different layers from the same GL_TEXTURE_ARRAY for use in a compute shader.</p>
<p>I am not sure if this is possible or if I am doing it correctly.</p>
<p>What I have tried is to first create an array of 2D textures for</p>
<pre><code>glGenTextures(1,&amp;im_mvs); glActiveTexture(GL_TEXTURE0+1);  
glBindTexture(GL_TEXTURE_2D_ARRAY,im_mvs);
// setting various parameters here
glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1 , GL_RGBA32F, lMatDims[0]/lSSFact, lMatDims[1]/lSSFact, NMVS);
glBindImageTexture(1, im_mvs, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
</code></pre>
<p>Then when setting up the shader call I do something like:</p>
<pre><code>glUseProgram(convProgram); // Now try to convolve.
glUniform1i(p4ImIn,1); glActiveTexture(GL_TEXTURE0+1); glBindImageTexture(1, im_mvs, 0, GL_FALSE, 0, GL_READ_ONLY,GL_RGBA32F);
glUniform1i(p4Filt,4); glActiveTexture(GL_TEXTURE0+4); glBindImageTexture(4, im_filt,0, GL_FALSE, 0, GL_READ_ONLY,GL_RGBA32F);
glUniform1i(p4ImOut,1); glActiveTexture(GL_TEXTURE0+1); glBindImageTexture(1, im_mvs, 0, GL_FALSE, 1, GL_WRITE_ONLY,GL_RGBA32F);
</code></pre>
<p>I currently can't get it to work, but as I still am quite new to opengl and glsl there is a fair chance that I may have made a mistake somewhere else.</p>
<p>The behavior that I am hoping for is to be able to select which layer of the array should be used in in-image and out-image respectively. This would make it very convenient to construct automated sequences of filterings by just changing the pointing with glBindImageTexture command like above.</p>
<p>I wonder if this is supposed to be possible or if I need to alter it somehow ?</p>
<hr />
<p><strong>Edit:</strong> Just as Nicol mentioned in the answer there were several issues with my code. The most important was that I had mixed up <em>texture units</em> and <em>image units</em>.</p>
<p>Inside my shader which defines uniform data like this :</p>
<pre><code>layout(rgba32f, binding = 0) uniform image2D im_in;
layout(rgba32f, binding = 1) uniform image2D filt;
layout(rgba32f, binding = 2) uniform image2D im_out;
</code></pre>
<p>The &quot;binding&quot; numbers there should correspond to the first argument in the call to <em>glBindImageTexture</em> as the types &quot;image2D&quot; is an image type. The glActiveTexture command is superflous as the variables im_mvs, im_filt already are tied to the texture(array)s which the image will be gotten from. I had them there previously to be able to transfer data.</p>
<p>I changed to</p>
<pre><code>glUniform1i(p4ImIn,0); glBindImageTexture(0, im_mvs, ...
glUniform1i(p4Filt,1); glBindImageTexture(1, im_filt,...
glUniform1i(p4ImOut,2); glBindImageTexture(2, im_mvs, ...
</code></pre>
<p>and now the code works as I had hoped. I will now go ahead to investigate various ways to filter my MVs. Thank you for the help.</p>

CC BY-SA 4.0
2023-01-31T22:44:40.330Z
`glActiveTexture(GL_TEXTURE0+1);` What do you think these calls are doing?
Nicol Bolas
<opengl><glsl><texture><compute-shader>
GLSL trying to bind two different layers from the same texture array as images. Should it work and if so what am I doing wrong?
mathreadler
# GLSL trying to bind two different layers from the same texture array as images. Should it work and if so what am I doing wrong?

(Asked by: username_0 on 2023-01-31)

I am new at this particular site in the SE ecosystem, but not to computer graphics in general. If I'm in the wrong spot feel free to redirect me.

My question regards glsl programming and binding two different layers from the same GL\_TEXTURE\_ARRAY for use in a compute shader.

I am not sure if this is possible or if I am doing it correctly.

What I have tried is to first create an array of 2D textures for

```
glGenTextures(1,&im_mvs); glActiveTexture(GL_TEXTURE0+1);  
glBindTexture(GL_TEXTURE_2D_ARRAY,im_mvs);
// setting various parameters here
glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1 , GL_RGBA32F, lMatDims[0]/lSSFact, lMatDims[1]/lSSFact, NMVS);
glBindImageTexture(1, im_mvs, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);

```

Then when setting up the shader call I do something like:

```
glUseProgram(convProgram); // Now try to convolve.
glUniform1i(p4ImIn,1); glActiveTexture(GL_TEXTURE0+1); glBindImageTexture(1, im_mvs, 0, GL_FALSE, 0, GL_READ_ONLY,GL_RGBA32F);
glUniform1i(p4Filt,4); glActiveTexture(GL_TEXTURE0+4); glBindImageTexture(4, im_filt,0, GL_FALSE, 0, GL_READ_ONLY,GL_RGBA32F);
glUniform1i(p4ImOut,1); glActiveTexture(GL_TEXTURE0+1); glBindImageTexture(1, im_mvs, 0, GL_FALSE, 1, GL_WRITE_ONLY,GL_RGBA32F);

```

I currently can't get it to work, but as I still am quite new to opengl and glsl there is a fair chance that I may have made a mistake somewhere else.

The behavior that I am hoping for is to be able to select which layer of the array should be used in in-image and out-image respectively. This would make it very convenient to construct automated sequences of filterings by just changing the pointing with glBindImageTexture command like above.

I wonder if this is supposed to be possible or if I need to alter it somehow ?

---

**Edit:** Just as username_1 mentioned in the answer there were several issues with my code. The most important was that I had mixed up *texture units* and *image units*.

Inside my shader which defines uniform data like this :

```
layout(rgba32f, binding = 0) uniform image2D im_in;
layout(rgba32f, binding = 1) uniform image2D filt;
layout(rgba32f, binding = 2) uniform image2D im_out;

```

The "binding" numbers there should correspond to the first argument in the call to *glBindImageTexture* as the types "image2D" is an image type. The glActiveTexture command is superflous as the variables im\_mvs, im\_filt already are tied to the texture(array)s which the image will be gotten from. I had them there previously to be able to transfer data.

I changed to

```
glUniform1i(p4ImIn,0); glBindImageTexture(0, im_mvs, ...
glUniform1i(p4Filt,1); glBindImageTexture(1, im_filt,...
glUniform1i(p4ImOut,2); glBindImageTexture(2, im_mvs, ...

```

and now the code works as I had hoped. I will now go ahead to investigate various ways to filter my MVs. Thank you for the help.

**Tags:** opengl, glsl, texture, compute-shader

> **Comments:**
> **username_1**: `glActiveTexture(GL_TEXTURE0+1);` What do you think these calls are doing?  

---

## 1 answers

### Answer by username_1 on 2023-01-31. Score: 2 | Accepted answer

There is no such thing as an "in image" and an "out image". There are just [images used for load/store operations](https://www.khronos.org/opengl/wiki/Image_Load_Store). A particular image can be write-only or read-only, but what "a particular image" *means* is defined by the binding index used in the shader. That is, there is exactly and only one image bound to index 1 at any time, with exactly and only one set of properties (readable, writeable, etc). If you bind another image to index 1, then you have *unbound* the previous image, and you have changed the properties of the binding.

Whatever `p4ImIn` and `p4ImOut` represent in your shader, they should have different binding indices. And therefore, you should be binding those images to different indices.

> **Comments:**
> **username_0**: Thank you username_1. Of course you are right. Sorry for the messy code. I just have the convention of naming targets for writing or store operations for "out" and targets for reading or load for "in". I realized just 1 hour ago I had mixed up image units and texture units.  

---

