Using different texture types in same texture unit at the same time in shader

cha*_*hma 6 c++ opengl textures glsl opengl-3

I came across a nasty problem in my program when i tried to use the same texture unit (number 0) for different texture types (i.e. a normal 2D texture and a cube map) in my shader. It appeared so that the GL issues a 502H (Invalid Operation) after the first glDrawArrays call. In my application code i load up the textures to different texture targets:

void setup_textures()
{
    unsigned int width, height;
    int components;
    unsigned int format;
    float param[8];
    vector<unsigned char> pngData;
    GLenum texture_target;

    glGenTextures(2, textures);

    glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, param);

    for(int i = 0; i < 2; i++) {
        texture_target = (i == 0) ? (GL_TEXTURE_CUBE_MAP) : (GL_TEXTURE_2D); // The first texture is the cube map

        glActiveTexture(GL_TEXTURE0);

        glBindTexture(texture_target, textures[i]);

        glTexParameterf(texture_target, GL_TEXTURE_MAX_ANISOTROPY_EXT, param[0]);
        glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
        glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_REPEAT);
        if(texture_target == GL_TEXTURE_CUBE_MAP) glTexParameteri(texture_target, GL_TEXTURE_WRAP_R, GL_REPEAT);

        loadPNG(pngData, width, height, PNGFile[i], LCT_RGBA, 8) // PNGFile[0] is my 2D texture file and PNGFile[1] is the cube texture

        if(texture_target == GL_TEXTURE_CUBE_MAP) {
            glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &pngData[0]);
            glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &pngData[0]);
            glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &pngData[0]);
            glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &pngData[0]);
            glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &pngData[0]);
            glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &pngData[0]);
        } else if(texture_target == GL_TEXTURE_2D)
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &pngData[0]);

        glGenerateMipmap(texture_target);
    }

    return;
}
Run Code Online (Sandbox Code Playgroud)

In my render function i bind the texture to its corresponding target and tell the shader which texture type to use (by means of a bool uniform):

void render(HDC hdc)
{
    GLenum texture_target;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    for(int i = 0; i < 2; i++) {
        texture_target = (i == 0) ? (GL_TEXTURE_CUBE_MAP) : (GL_TEXTURE_2D);

        glBindVertexArray(objVertexArray[i]);

        glActiveTexture(GL_TEXTURE0);

        glBindTexture(texture_target, textures[i]);

        glUniform1i(uniIsCubeMap, texture_target == GL_TEXTURE_CUBE_MAP); // Tell the shader the texture type

        glDrawArrays(GL_TRIANGLES, 0, nVertices[i]);
    }

    SwapBuffers(hdc);

    return;
}
Run Code Online (Sandbox Code Playgroud)

在片段着色器中,纹理类型基于is_cube_map uniform确定:

#version 330 core

uniform sampler2D texture_map;
uniform samplerCube texture_map_cube;
uniform bool is_cube_map;

smooth in vec3 texcoords;

out vec4 fragcolor;

void main(void)
{
    vec4 texel;

    if(is_cube_map) {
        texel = textureCube(texture_map_cube, texcoords.stp);
    } else {
        texel = texture(texture_map, texcoords.st);
    }

    fragcolor = texel;
}
Run Code Online (Sandbox Code Playgroud)

我还在我的应用程序代码中将两个纹理采样器制服设置为0(纹理单元号0):

glUniform1iARB(uniTextureMap, 0);
glUniform1iARB(uniTextureMapCube, 0);
Run Code Online (Sandbox Code Playgroud)

会出现什么问题?真的不是一件有效的事吗?

Nic*_*las 6

是的,这不是一件有效的事情.

您应该完全忘记OpenGL甚至允许您将不同的纹理目标绑定到相同的纹理单元.对于这种能力,你无能为力.如果需要绑定纹理以对其进行修改,请在之后取消绑定.如果需要绑定纹理以将其用于渲染,则不应将任何其他内容绑定到该纹理单元.

在任何情况下,您的绘图函数都应该已经获得GL_INVALID_OPERATION,因为同一程序/管道中的两个采样器引用相同的纹理单元,但使用不同的采样器类型是非法的.