[LWJGL] Sending multiple textures to sampler uniforms in openGL

By convention, I’ve read on the internet and put into practice that to send a texture to a shader via sampler2D, you need to:


glActiveTexture(GL13.GL_TEXTURE0 + textureUnit);
glBindTexture(GL_TEXTURE_2D, textureId);
glUniform1i(uniform, textureUnit);

But… why do we need textureUnit? Surely there must be some way of sending exactly the texture data to the shader without having to bind it to a texture unit… To me this just seems like an extra little bit of faff that isn’t exactly needed…

Could someone explain?

Why couldn’t something like this exist:


glUniformSampler(glGetUniformLocation(shaderProgramId, "uSampler1"), texture1id);
glUniformSampler(glGetUniformLocation(shaderProgramId, "uSampler2"), texture2id);
glUniformSampler(glGetUniformLocation(shaderProgramId, "uSampler3"), texture3id);

Latest GLSL versions allows this

frag:


layout(binding=0) uniform sampler2D diffuseTex

the 0 is the slot.

java where you bind:


GL13.glActiveTexture(GL13.GL_TEXTURE0 + slot);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, getId());

It used to be a hardware limitation. GPUs were limited by the number of texture units they had, as they had their own caches and stuff like that. When compute became more important, they started with more generic shared caches, so texture units no longer exist in hardware. What you’re after is bindless textures.