Search code examples
c++openglglslshader

bindTextureUnit with Image storage


I was unable to find any documentation for this but is it possible to do:

layout(RGBA8, binding = 1) uniform image3D voxels;

in a fragment shader and bind it in C++ using glBindTextureUnit(1, myTexture); ?

Or do I have to query the uniform location manually?


Solution

  • I was unable to find any documentation for this but is it possible to do [...]

    Yes.

    Or do I have to query the uniform location manually?

    No.

    But glBindTextureUnit is the wrong instruction. An image uniform is linked to a texture object by the texture image unit.

    In the shader program the the binding points can be set:

    layout(RGBA8, binding = 1) uniform image3D voxels;
    

    this is the equivalent of getting the uniform location for "voxels" and setting its uniform value to "1". Hence the image uniform voxels, is associated to texture image unit "1".

    The texture object has to be bound to the corresponding texture image unit by glBindImageTexture. e.g:

    glBindImageTexture(1, myTexture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA8); 
    

    this instruction binds myTexture to texture image unit 1.


    Note, glBindTextureUnit(1, myTexture); would bind a texture object to a texture unit, which can be use by a corresponding texture sampler uniform. e.g:

    layout(binding = 1) uniform sampler3D voxels;