From what I gather, glActiveTexture
sets the active "texture unit". Each texture unit can have multiple texture targets (usually GL_TEXTURE_1D, 2D, 3D or CUBE_MAP).
If I understand correctly, you have to call glActiveTexture
to set the texture unit first (initialized to GL_TEXTURE0
), and then you bind (one or more) "texture targets" to that texture unit?
The number of texture units available is system dependent. I see enums for up to 32 in my library. I guess this essentially means I can have the lesser of my GPU's limit (which I think is 16 8) and 32 textures in GPU memory at any one time? I guess there's an additional limit that I don't exceed my GPU's maximum memory (supposedly 1 GB).
Am I understanding the relationship between texture targets and texture units correctly? Let's say I'm allowed 16 units and 4 targets each, does that mean there's room for 16*4=64 targets, or does it not work like that?
Next you typically want to load a texture. You can do this via glTexImage2D
. The first argument of which is a texture target. If this works like glBufferData
, then we essentially bind the "handle"/"texture name" to the texture target, and then load the texture data into that target, and thus indirectly associate it with that handle.
What about glTexParameter
? We have to bind a texture target, and then choose that same target again as the first argument? Or does the texture target not need to be bound as long as we have the correct active texture unit?
glGenerateMipmap
operates on a target too...that target has to still be bound to the texture name for it to succeed?
Then when we want to draw our object with a texture on it, do we have to both choose an active texture unit, and then a texture target? Or do we choose a texture unit, and then we can grab data from any of the 4 targets associated with that unit? This is the part that's really confusing me.
If in your shader you need lookup from 2 textures:
it is necessary to indicate for tex1 and tex2 their sources as follows :
in the render loop :
With a gl_bindtexture, not possible to do such a thing. On the other hand a possible use of a bind in the loop of rendering, is the case where you feed a texture with a content in stream (video, webcam) :
I'll give it a try ! All this is not that complicated, just a question of terms, hope I'll make myself clear.
You can create roughly as many Texture Objects as there is available memory in your system. These objects hold the actual data (texels) of your textures, along with parameters, provided by glTexParameter (see FAQ).
When being created, you have to assign one Texture Target to one texture object, which represents the type of the texture (
GL_TEXTURE_2D
,GL_TEXTURE_3D
,GL_TEXTURE_CUBE
, ...).These two items, texture object and texture target represent the texture data. We'll come back to them later.
Texture units
Now, OpenGL provides an array of texture units, that can be used simultaneously while drawing. The size of the array depends of the OpenGL system, yours has 8.
You can bind a texture object to a texture unit to use the given texture while drawing.
In a simple and easy world, to draw with a given texture, you'd bind a texture object to the texture unit, and you'd do (pseudocode):
As GL is a state machine, it, alas, does not work this way. Supposing that our
textureObject
has data for theGL_TEXTURE_2D
texture target, we'll express the previous assignment as :Note that
GL_TEXTURE_2D
really depends on the type of the texture you want to bind.Texture objects
In pseudo code, to set texture data or texture parameters, you'd do for example :
OpenGL can't directly manipulate texture objects, to update/set their content, or change their parameters, you have to first bind them to the active texture unit (whichever it is). The equivalent code becomes :
Shaders
Shaders have access to all the texture units, they don't care about the active texture.
Sampler uniforms are
int
values representing the index of the texture unit to use for the sampler (and not the texture object to use).So you have to bind your texture objects to the units you want to use.
The type of the sampler will do the match with the texture target that is used in the texture unit :
Sampler2D
forGL_TEXTURE_2D
, and so on...All About OpenGL Objects
The standard model for OpenGL objects is as follows.
Objects have state. Think of them as a
struct
. So you might have an object defined like this:The object has certain values stored in it and it has state. OpenGL objects have state too.
Changing State
In C/C++, if you have an instance of type
Object
, you would change its state as follows:obj.count = 5;
You would directly reference an instance of the object, get the particular piece of state you want to change, and shove a value into it.In OpenGL, you don't do this.
For legacy reasons better left unexplained, to change the state of an OpenGL object, you must first bind it to the context. This is done with some from of
glBind*
call.The C/C++ equivalent to this is as follows:
Textures are interesting; they represent a special case of binding. Many
glBind*
calls have a "target" parameter. This represents different locations in the OpenGL context where objects of that type can be bound. For example, you can bind a framebuffer object for reading (GL_READ_FRAMEBUFFER
) or for writing (GL_DRAW_FRAMEBUFFER
). This affects how OpenGL uses the buffer. This is what theloc
parameter above represents.Textures are special because when you first bind them to a target, they get special information. When you first bind a texture as a
GL_TEXTURE_2D
, you are actually setting special state in the texture. You are saying that this texture is a 2D texture. And it will always be a 2D texture; this state cannot be changed ever. If you have a texture that was first bound as aGL_TEXTURE_2D
, you must always bind it as aGL_TEXTURE_2D
; attempting to bind it asGL_TEXTURE_1D
will give rise to an error (while run-time).Once the object is bound, its state can be changed. This is done via generic functions specific to that object. They too take a location that represents which object to modify.
In C/C++, this looks like:
Notice how this function sets whatever happens to be in currently bound
loc
value.For texture objects, the main texture state changing functions are
glTexParameter
. The only other functions that change texture state are theglTexImage
functions and their variations (glCompressedTexImage
,glCopyTexImage
, the recentglTexStorage
). The variousSubImage
versions change the contents of the texture, but they do not technically change its state. TheImage
functions allocate texture storage and set the texture's format; theSubImage
functions just copy pixels around. That is not considered the texture's state.Allow me to repeat: these are the only functions that modify texture state.
glTexEnv
modifies environment state; it doesn't affect anything stored in texture objects.Active Texture
The situation for textures is more complex, again for legacy reasons best left undisclosed. This is where
glActiveTexture
comes in.For textures, there aren't just targets (
GL_TEXTURE_1D
,GL_TEXTURE_CUBE_MAP
, etc). There are also texture units. In terms of our C/C++ example, what we have is this:Notice that now, we not only have a 2D list of
Object
s, but we also have the concept of a current object. We have a function to set the current object, we have the concept of a maximum number of current objects, and all of our object manipulation functions are adjusted to select from the current object.When you change the currently active object, you change the entire set of target locations. So you can bind something that goes into current object 0, switch to current object 4, and will be modifying a completely different object.
This analogy with texture objects is perfect... almost.
See,
glActiveTexture
does not take an integer; it takes an enumerator. Which in theory means that it can take anything fromGL_TEXTURE0
toGL_TEXTURE31
. But there's one thing you must understand:THIS IS FALSE!
The actual range that
glActiveTexture
can take is governed byGL_MAX_COMBINED_TEXTURE_IMAGE_UNITS
. That is the maximum number of simultaneous multitextures that an implementation allows. These are each divided up into different groupings for different shader stages. For example, on GL 3.x class hardware, you get 16 vertex shader textures, 16 fragment shader textures, and 16 geometry shader textures. Therefore,GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS
will be 48.But there aren't 48 enumerators. Which is why
glActiveTexture
doesn't really take enumerators. The correct way to callglActiveTexture
is as follows:where
i
is a number between 0 andGL_MAX_COMBINED_TEXTURE_IMAGE_UNITS
.Rendering
So what does all of this have to do with rendering?
When using shaders, you set your sampler uniforms to a texture image unit (
glUniform1i(samplerLoc, i)
, wherei
is the image unit). That represents the number you used withglActiveTexture
. The sampler will pick the target based on the sampler type. So asampler2D
will pick from theGL_TEXTURE_2D
target. This is one reason why samplers have different types.Now this sounds suspiciously like you can have two GLSL samplers, with different types that use the same texture image unit. But you can't; OpenGL forbids this and will give you an error when you attempt to render.
Imagine the GPU like some paint processing plant.
There are a number of tanks, which delivers dye to some painting machine. In the painting machine the dye is then applied to the object. Those tanks are the texture units
Those tanks can be equipped with different kinds of dye. Each kind of dye requires some other kind of solvent. The "solvent" is the texture target. For convenience each tank is connected to some solvent supply, and but only one kind of solvent can be used at a time in each tank. So there's a valve/switch
TEXTURE_CUBE_MAP
,TEXTURE_3D
,TEXTURE_2D
,TEXTURE_1D
. You can fill all the dye types into the tank at the same time, but since only one kind of solvent goes in, it will "dilute" only the kind of dye matching. So you can have each kind of texture being bound, but the binding with the "most important" solvent will actually go into the tank and mix with the kind of dye it belongs to.And then there's the dye itself, which comes from a warehouse and is filled into the tank by "binding" it. That's your texture.