Correspondance between texture units and sampler uniforms in opengl

后端 未结 2 1821
耶瑟儿~
耶瑟儿~ 2021-01-22 11:01

The correspondence between sampler uniforms and texture units used by glActiveTexture apparently can\'t be queried with opengl, and I can\'t find good documentation

2条回答
  •  醉酒成梦
    2021-01-22 11:20

    You have to set the index of the texture unit to sampler uniform (similar as setting the value of a uniform variable of type int). e.g. value 1 for GL_TEXTURE1.

    See OpenGL 4.6 API Compatibility Profile Specification; 7.10 Samplers; page 154:

    Samplers are special uniforms used in the OpenGL Shading Language to identify the texture object used for each texture lookup. The value of a sampler indicates the texture image unit being accessed. Setting a sampler’s value to i selects texture image unit number i.

    e.g.

    layout (location = 11) uniform sampler2D color;
    layout (location = 12) uniform sampler2D tex;
    layout (location = 13) uniform sampler2D norm;
    
    glUniform1i(11, 0); // 0: GL_TEXTURE0
    glUniform1i(12, 1); // 1: GL_TEXTURE1
    glUniform1i(13, 2); // 2: GL_TEXTURE2
    

    Since GLSL version 4.2 this can be done in the fragment shader by specifying binding points - See OpenGL Shading Language 4.20 Specification - 4.4.4 Opaque-Uniform Layout Qualifiers; page 60:

    #version 420
    
    layout (binding = 0) uniform sampler2D color;
    layout (binding = 1) uniform sampler2D tex;
    layout (binding = 2) uniform sampler2D norm;
    

提交回复
热议问题