I’m having some trouble understanding how texture binding really works. Of course it is simple when you’re dealing with just one texture, but when more textures come into play, the official documentation isn’t really clear on how the state changes.
First of all, how are the texture bindings represented in the state? I imagine it’s something similar to this, in pseudo-code:
struct texture_state {
GLuint texture_unit_bindings[UNIT_COUNT][TARGET_COUNT];
GLenum current_texture_unit;
};
Then, when glActiveTexture
is called something like this probably happens:
void glActiveTexture_impl(struct texture_state *tex_state, GLenum texture) {
tex_state->current_texture_unit = texture;
}
So far the docs are clear, but the part that I don’t quite understand is how glBindTexture
affects the state. So given it would affect state like this:
void glBindTexture_impl(struct texture_state *tex_state, GLenum target, GLuint texture) {
tex_state->texture_unit_bindings[tex_state->current_texture_unit][target] = texture;
}
I suppose that other targets are not updated? What about shader usage, are these two uniform variables referring to different texture targets in the same unit, or should I bind only one texture target to a given texture unit at a time?
uniform sampler2D my_2d_texture;
uniform sampler3D my_3d_texture;
GLint loc_my_2d_texture = glGetUniformLocation(program, "my_2d_texture");
GLint loc_my_3d_texture = glGetUniformLocation(program, "my_3d_texture");
glUseProgram(program);
glUniform1i(loc_my_2d_texture, 0);
glUniform1i(loc_my_3d_texture, 0);