Bind a TEXTURE_2D to a texture unit without it bound to the rendering context

Hello !

I’m currently working with fragment shaders, and I was wondering how I could bind a texture to a uniform while the context’s bound texture is not this specific texture. For instance, my code for bounding the texture to the texture unit is :

glUniform1i(uniform, textures.get(name));
glActiveTexture(GL_TEXTURE0 + textures.get(name));
glBindTexture(GL_TEXTURE_2D, tex.texture.get(0));

Except that since the active texture is GL_TEXTURE0 + textures.get(name), if i then call glBindTexture() again, the newly bound texture is set as the uniform.

Do I have to then “move” the active texture to an unused texture unit index if I want to use glBindTexture() again without it influencing the sampler2D ? or is there a cleaner way to do this (such as binding while not setting as a texture unit).

Thanks !

Actually, after some digging I think that I understand that actually the texture bound to the context is GL_TEXTURE0. Is that right ? If so I’d just need to add 1 to all indices and then reset the active texture to GL_TEXTURE0 (which seems to work, I just want to be sure that I have the correct understanding).

Thanks !

Your question is a contradiction. Texture units are part of the rendering context; you cannot bind a texture to a texture unit without it also being bound to the rendering context.

Bindless texturing allows you to use a texture without binding it to a context, but that’s about not binding it at all.

Do I have to then “move” the active texture to an unused texture unit index if I want to use glBindTexture() again without it influencing the sampler2D ?

Pretty much, but you would only need to do that if you’re modifying the texture. There are ways to do that without binding it nowadays, but you should avoid modifying a texture while doing general rendering operations (ie: the time period when it matters what textures are bound to which units). You should modify textures before rendering, then render with them, binding textures to units as needed.

Sorry, rendering context was a poor choice of words. What I meant is if I’m rendering a texture in such a way :

glBindTexture(GL_TEXTURE_2D, tex);

glTexCoord2f(0, 0);
glVertex2f(x0, y0);
glTexCoord2f(1, 0);
glVertex2f(x1, y0);
glTexCoord2f(1, 1);
glVertex2f(x1, y1);
glTexCoord2f(0, 1);
glVertex2f(x0, y1);

Then which texture unit is used ? is it GL_TEXTURE0 or only the one currently active ? Because it also seems that the texture rendered this way is the one bound to GL_TEXTURE0 (which is to say, if the active texture is not GL_TEXTURE0, then calling glBindTexture(GL_TEXTURE_2D, tex) doesn’t make the specified texture rendered, but the one rendered is the one that was previously bound to GL_TEXTURE0)

Thanks for your help :slight_smile:

That depends on what you mean by “used”.

The bind call will bind the texture to whatever texture unit is current at the time the call is made. So whatever the most recent glActiveTexture call was defines the unit to which the texture is bound.

Which texture gets used for rendering is a different matter. Since you’re using outdated compatibility GL stuff, which texture unit gets used depends on how you’ve set up your texture environment (TEV) state. TEV state processes texture units from first to last, and you enable some number of texture units for TEV processing. So if you have enabled 3 TEV stages, then texture units on the range [0, 2] will be used. If tex was bound to one of those stages, then the math computed by the TEV operations will use tex to fill in its part of the equation.