c++opengl

Invalid operation in `glTextureView`


I am using a GL_TEXTURE_2D_ARRAY for storing all my textures and was trying to figure out a TextureView system to see individual textures, but when I try to initialize it, it says GL_INVALID_OPERATION in glTextureView. Here is my code for creating the texture array and texture view.

TextureArray::TextureArray(int _width, int _height, int _layers) {
    this->width = _width;
    this->height = _height;
    this->layers = _layers;
    this->slot = 0;
    glCreateTextures(GL_TEXTURE_2D_ARRAY, 1, &handle);
    glTextureStorage3D(handle, static_cast<int>(1 + std::floor(std::log2(std::max(width, height)))), GL_RGBA8, width, height, layers);

    glTextureParameteri(handle, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTextureParameteri(handle, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTextureParameteri(handle, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTextureParameteri(handle, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    if (maxAniso == -1) glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY, &maxAniso);
    glTextureParameterf(handle, GL_TEXTURE_MAX_ANISOTROPY, maxAniso);

    glCreateTextures(GL_TEXTURE_2D, 1, &layerView);
    glTextureView(
        layerView,            // new texture ID
        GL_TEXTURE_2D,        // target: treat as 2D
        handle,               // source array
        GL_RGBA8,             // internal format
        0, 1,                 // mip levels
        0, 1                  // base layer, number of layers (only 1 layer)
    );
}

Is there any issue in the glTextureView function?


Solution

  • Reference about glTextureView clearly states that texture object treated as a view cannot be created by glCreateTextures:

    GL_INVALID_VALUE is generated if texture zero or is not the name of a texture previously returned from a successful call to glGenTextures.

    More info about views you can get from https://www.khronos.org/opengl/wiki/Texture_Storage, where is:

    Warning: The texture​ parameter must be a name returned from glGenTextures that has not previously been bound to a target.

    This function requires an uninitialised texture, so using names fromglCreateTextureor names that have been bound already will fail.