3

Quick OpenGL newbie question. I have this line in my fragment shader:

uniform sampler2D mytexture;

I grepped into the sample code I'm using and I couldn't find any reference to mytexture; only found calls which activate a texture unit and bind a texture to it that was previously copied to the GPU.

My question is, how does the fragment shader know that the only texture I'm using has to be referred via mytexture?

I would have thought a

glBindTextureToUniform(texture_id, "mytexture");

or similar had to be called.

1

2 Answers 2

7

The mapping is between the texture unit and the uniform but not the texture, as concept3d mentions. The reason for this discrepancy is multi-texturing.

Texturing units is the machinery in OpenGL to facilitate multi-texturing. When you want multiple textures to be looked-up in your shaders, you bind each to a different texture unit (actually you bind it to a target within the unit) and have different uniforms for each to refer them to in a shader. Each texturing unit has multiple targets like 1D, 2D, 3D, etc.

The texture looked-up in the shader, through the uniform, refers to the unit. Its type (e.g. sampler2D) decides the target in the unit that is used. Here is how the mapping is

uniform sampler2D mytexture;
            |         |
            |         |
            |         +--->GL_TEXTURE0 / UNIT 0      GL_TEXTURE1 / UNIT 1      GL_TEXTURE2 / UNIT 2
            |             +--------------------+    +--------------------+    +--------------------+
            |             |   +------------+   |    |   +------------+   |    |   +------------+   |
            |             |   | TEXTURE_1D |   |    |   | TEXTURE_1D |   |    |   | TEXTURE_1D |   |
            |             |   +------------+   |    |   +------------+   |    |   +------------+   |
            |             |   +------------+   |    |   +------------+   |    |   +------------+   |
            +---------------->| TEXTURE_2D |   |    |   | TEXTURE_2D |   |    |   | TEXTURE_2D |   |
                          |   +------------+   |    |   +------------+   |    |   +------------+   |
                          |   +------------+   |    |   +------------+   |    |   +------------+   | …
                          |   | TEXTURE_3D |   |    |   | TEXTURE_3D |   |    |   | TEXTURE_3D |   |
                          |   +------------+   |    |   +------------+   |    |   +------------+   |
                          |   +------------+   |    |   +------------+   |    |   +------------+   |
                          |   |TEX_1D_ARRAY|   |    |   |TEX_1D_ARRAY|   |    |   |TEX_1D_ARRAY|   |
                          |   +------------+   |    |   +------------+   |    |   +------------+   |
                          |         .          |    |         .          |    |         .          |
                          |         .          |    |         .          |    |         .          |
                          |         .          |    |         .          |    |         .          |
                          +--------------------+    +--------------------+    +--------------------+

It's to these targets (the inner boxes) to which you upload the texture data from an image or a table, etc. The active unit, if not set explicitly, is by default GL_TEXTURE0.

how does the fragment shader know that the only texture I'm using has to be referred via mytexture in the fragment shader?

It's better to set the uniform with the unit ID. If you did not and it still works, then you are depending on the forgiving behaviour of your graphics driver. It may not work on all devices or even a future version of the driver. We will have to look up the spec. to know if this is allowed.

1
6

OpenGL has the concept texture units, those are fixed starting from 0 to MAX_TEXTURE_UNIT.

  1. First you select the active texture unit, using glActiveTexture(GL_TEXTURE0)
  2. Then you bind the texture to the active texture unit.using glBindTexture(GL_TEXTURE_2D, tex);
  3. And the finally you set the shader uniform value to reference the texture unit not the the texture id, it uses whatever texture was bound to the unit.

Think of the texture unit as a container, the uniform variable use whatever is in the texture unit.

In other words,

  • You bind a texture Id to a texture unit. not glBindTextureToUniform directly.

  • You set the uniform variable to the value of the texture unit and it will reference whatever texture was bound to it.

1
  • Upvoted for the third point; a stumbling block for many beginners.
    – legends2k
    Dec 23, 2015 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.