Results 1 to 7 of 7

Thread: What is a 'texture unit'?

  1. #1
    Member Contributor
    Join Date
    Dec 2010
    Posts
    57

    What is a 'texture unit'?

    I guess what I really want to know is there 1 texture per texture unit? Or can I generate multiple textures in the same texture unit? Thanks.

  2. #2
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,675

    Re: What is a 'texture unit'?

    I guess what I really want to know is there 1 texture per texture unit? Or can I generate multiple textures in the same texture unit?
    These two questions have nothing to do with each other, and the fact that you asked them both at the same time betrays a misunderstanding of how texturing works.

    When you execute the following code:

    Code :
    GLuint tex;
    glGenTextures(1, &tex);

    You have (for all intents and purposes) created a texture. You have created a space that will hold all of the data associated with a texture. And the variable 'tex' now stores the reference to this texture.

    When you call:

    Code :
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, tex);

    You are doing a number of things. You are attaching the texture object 'tex' to the texture unit number 0. You are telling OpenGL that the texture object 'tex' is a 2D texture, and you will refer to it (while it is bound) with GL_TEXTURE_2D.

    When you call:

    Code :
    glTexImage(GL_TEXTURE_2D, ...);

    You are telling OpenGL to create image data, associate that image data with the texture that is currently bound to the active texture unit (GL_TEXTURE0) and is bound to the GL_TEXTURE_2D target. When called immediately after our previous functions, that will refer to the texture object 'tex'.

    So this function will cause image data to be attached to the texture object 'tex'. But only because it is the texture currently bound to the current active texture unit and the GL_TEXTURE_2D target.

    Now, when you call:

    Code :
    glBindTexture(GL_TEXTURE_2D, 0);

    You are telling OpenGL that the currently bound texture object (aka: 'tex') is no longer bound to the current texture unit and GL_TEXTURE_2D target. This means that if you call glTexImage again, it will not affect the texture object 'tex'. Not unless you bind 'tex' again.

    In short, 'tex' is a free-standing memory object. It stores all of the data associated with the texture. Unbinding it from a texture unit will have no affect on the contents of the object.

    You can repeat this process for as long as you like. Well, until OpenGL runs out of memory. You can create textures with glGenTextures, bind them, create image data for them, and unbind them.

    Now, when it comes time to use textures as source for image data, you must bind them again. But only the ones you need for that particular drawing operation.

    Let's say you have object A and object B. Object A needs texture 'texA' bound to texture unit 0, and object B needs textures 'texB' and 'texC', bound to texture units 0 and 1 respectively.

    To render object A, you do this:

    Code :
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texA);
    glEnable(GL_TEXTURE_2D); //Only use this if not using shaders.
     
    glDrawElements(...);
     
    //Cleanup texture binds.
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, 0);
    glEnable(GL_TEXTURE_2D); //Only use this if not using shaders.

    To render object B, you do this:

    Code :
    glActiveTexture(GL_TEXTURE0 + 0);
    glBindTexture(GL_TEXTURE_2D, texB);
    glEnable(GL_TEXTURE_2D); //Only use this if not using shaders.
    glActiveTexture(GL_TEXTURE0 + 1);
    glBindTexture(GL_TEXTURE_2D, texC);
    glEnable(GL_TEXTURE_2D); //Only use this if not using shaders.
     
    glDrawElements(...);
     
    //Cleanup texture binds.
    glActiveTexture(GL_TEXTURE0 + 0);
    glBindTexture(GL_TEXTURE_2D, 0);
    glEnable(GL_TEXTURE_2D); //Only use this if not using shaders.
    glActiveTexture(GL_TEXTURE0 + 1);
    glBindTexture(GL_TEXTURE_2D, 0);
    glEnable(GL_TEXTURE_2D); //Only use this if not using shaders.

    Does that answer your question? Even though binding a texture to a texture unit is necessary for creating it and for using it, that does not mean that texture units and texture objects are permanently associated with each other.

    If we wanted, when rendering objectB, 'texA' could have been bound to texture unit 1 instead of 'texC'.

  3. #3
    Member Contributor
    Join Date
    Sep 2009
    Posts
    55

    Re: What is a 'texture unit'?

    You can think of the "texture unit" as "a piece of hardware that takes a sample of a texture" (using on texture coordinates). When drawing a particular object, each texture unit can sample one texture, but you can change the texture each sampler uses in between drawing objects.

    The details of how texture sampling is controlled depends on whether you are using fixed-function OpenGL or GLSL for your fragment processing.

  4. #4
    Member Contributor
    Join Date
    Dec 2010
    Posts
    57

    Re: What is a 'texture unit'?

    I think I almost have the idea. I always assumed that the texture coordinates would refer to the active texture unit. So how do I specify which texture unit the texture coords are for? Is that what specifying a uniform sampler is doing?:

    loc = glGetUniformLocation( mShaderProgramHandle, "texsampler" );
    glUniform1i( loc, 0);


    IE, if I changed it to glUniform1i( loc, 1);, the texture coords would refer to unit 1 instead?

  5. #5
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,675

    Re: What is a 'texture unit'?

    Is that what specifying a uniform sampler is doing?:
    No. You should have said you were using shaders to begin with. That changes things.

    The texture coordinates that are used to sample a texture are defined by the shader. The shader doesn't have to use texture coordinates passed as vertex attributes; it can make them up or generate them from arbitrary code.

    When using shaders, the only thing the texture unit does is define which texture is used. By setting a sampler uniform to point to a particular texture unit, you are saying that any use of that sampler in the shader refers to the texture bound to that texture unit.

  6. #6
    Member Contributor
    Join Date
    Sep 2009
    Posts
    55

    Re: What is a 'texture unit'?

    My experience with the programmable pipeline is 99% theoretical at this point, since I've worked mostly with fixed function.

    But here's how I read it:

    Quote Originally Posted by PrestoChung
    So how do I specify which texture unit the texture coords are for? Is that what specifying a uniform sampler is doing?
    Not quite. The association of texture coords with a texture unit is done in the shader. The uniform calls just set values that can be used in the shader:

    e.g., in
    Code :
    vec4 texture2D (sampler2D sampler, vec2 coord)
    the "sampler" variable specifies the texture unit you use, and "coord", the texture coordinate, comes from some appropriate attribute you fed into your shader. This call in the shader (or one like it) is how the GL knows which samplers get used with what texture coordinates.

    Quote Originally Posted by PrestoChung
    loc = glGetUniformLocation( mShaderProgramHandle, "texsampler" );
    glUniform1i( loc, 0);

    if I changed it to glUniform1i( loc, 1);, the texture coords would refer to unit 1 instead?
    Yes, if you use "texsampler" as the texture unit selector for a texture lookup function in your shader code.

    The actual texture the unit will sample is controlled by glBindTexture (using glActiveTexture to pick the unit you will bind to).

    EDIT: Prettify quotes and code
    EDIT: Add context to 2nd quote

  7. #7
    Member Contributor
    Join Date
    Dec 2010
    Posts
    57

    Re: What is a 'texture unit'?

    I see, thanks

Similar Threads

  1. finding out the active texture from the texture unit
    By minidrive in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 04-06-2013, 07:55 AM
  2. Depth texture bound to FBO and texture unit?
    By imported_pjcozzi in forum OpenGL: Advanced Coding
    Replies: 10
    Last Post: 02-01-2011, 05:14 PM
  3. Texture unit 0 blending with Texture unit 1 problem
    By kackurot in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 10-31-2006, 10:16 PM
  4. Multiple 3D Texture on different texture unit question
    By SW in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 06-04-2002, 06:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean