glBindTextureUnit
Puts a texture on texture unit number unit, in one call (no glActiveTexture).
Part of the family glBind*
Signature
void glBindTextureUnit(GLuint unit, GLuint texture);
How it works
Shaders read textures through numbered units: a sampler2D uniform holds a unit number, and whatever texture is on that unit at draw time is what it samples. The old way took two calls, glActiveTexture(GL_TEXTURE0 + unit) then glBindTexture(target, tex). This one does both, and the target is taken from the texture itself.
Parameters
The unit as a plain number: 0, 1, 2… NOT GL_TEXTURE0.
The texture, or 0 to unbind every target of that unit.
Tips & pitfalls
Direct State Access (GL 4.5): the object is passed as a parameter, so nothing needs to be bound and no global state is disturbed.
Passing GL_TEXTURE0 + 3 instead of 3 is the classic mistake: it asks for unit 33987, which does not exist.
The sampler uniform must hold the same number: glProgramUniform1i(prog, loc, 3), or layout(binding = 3) in GLSL.
Errors
When the call is invalid it does nothing and sets an error, which glGetError returns:
- GL_INVALID_OPERATION
- texture is not 0 and not an existing texture.
Example
1// GLSL: layout(binding = 3) uniform sampler2D albedo;
2glBindTextureUnit(3, tex);