上传压缩纹理时GL_INVALID_OPERATION

时间:2014-10-04 14:33:13

标签: c++ qt opengl texture2d

每次尝试使用Qt GL_INVALID_OPERATION上传具有多个mipmap级别的DXT1纹理时,我都会收到QOpenGLTexture。对于mipmap级别0,一切正常,但对于任何其他我在Qt库中的GL_INVALID_OPERATION得到glCompressedTexSubImage2DEXT()。我的错误在哪里?

代码:

QOpenGLVertexArrayObject::Binder vaoBinder(&mVAO);
bindProgram();
glActiveTexture(GL_TEXTURE0);

const dds::Image *base = parser.getImageData(0);

mTexture.setFormat(QOpenGLTexture::RGB_DXT1);
mTexture.setSize(base->width, base->height);
mTexture.setMipLevelRange(0, parser.getMipmapCount());
mTexture.allocateStorage();
mTexture.bind();

for (size_t i = 0; i < parser.getMipmapCount(); i++)
{
    // Image data represents a single mipmap level of a texture.
    const dds::Image *img = parser.getImageData(i);

    mTexture.setCompressedData(i, img->dataSize, img->data);
    qDebug() << "size: " << img->width << img->height;
    qDebug() << "i: " << i << " error: " << glGetError();
}

mTexture.setMinMagFilters(QOpenGLTexture::Linear, QOpenGLTexture::Linear);
mProgram.setUniformValue("objectTexture", 0);

unbindProgram();

这是程序日志:

size:  2048 2048
i:  0  error:  0
size:  1024 1024
i:  1  error:  1282
size:  512 512
i:  2  error:  1282
size:  256 256
i:  3  error:  1282
...
size:  1 1
i:  11  error:  1282

以下是glIntercept日志的一部分:

glGenTextures(1,00000000006DCE68)
glTextureParameteriEXT(1,GL_TEXTURE_2D,GL_TEXTURE_BASE_LEVEL,0)
glTextureParameteriEXT(1,GL_TEXTURE_2D,GL_TEXTURE_MAX_LEVEL,11)
glTextureStorage2DEXT(1,GL_TEXTURE_2D,1,GL_COMPRESSED_RGB_S3TC_DXT1_EXT,2048,2048)
glBindTexture(GL_TEXTURE_2D,1)
glCompressedTextureSubImage2DEXT(1,GL_TEXTURE_2D,0,0,0,2048,2048,GL_COMPRESSED_RGB_S3TC_DXT1_EXT,2097152,00000000147A0080)
glGetError()=GL_NO_ERROR 
glCompressedTextureSubImage2DEXT(1,GL_TEXTURE_2D,1,0,0,1024,1024,GL_COMPRESSED_RGB_S3TC_DXT1_EXT,524288,00000000149A0080) glGetError() = GL_INVALID_OPERATION
glGetError()=GL_INVALID_OPERATION 
glCompressedTextureSubImage2DEXT(1,GL_TEXTURE_2D,2,0,0,512,512,GL_COMPRESSED_RGB_S3TC_DXT1_EXT,131072,0000000014A20080) glGetError() = GL_INVALID_OPERATION
glGetError()=GL_INVALID_OPERATION 
glCompressedTextureSubImage2DEXT(1,GL_TEXTURE_2D,3,0,0,256,256,GL_COMPRESSED_RGB_S3TC_DXT1_EXT,32768,0000000014A40080) glGetError() = GL_INVALID_OPERATION
glGetError()=GL_INVALID_OPERATION 
glCompressedTextureSubImage2DEXT(1,GL_TEXTURE_2D,4,0,0,128,128,GL_COMPRESSED_RGB_S3TC_DXT1_EXT,8192,0000000014A48080) glGetError() = GL_INVALID_OPERATION
glGetError()=GL_INVALID_OPERATION 
glCompressedTextureSubImage2DEXT(1,GL_TEXTURE_2D,5,0,0,64,64,GL_COMPRESSED_RGB_S3TC_DXT1_EXT,2048,0000000014A4A080) glGetError() = GL_INVALID_OPERATION
glGetError()=GL_INVALID_OPERATION 
glCompressedTextureSubImage2DEXT(1,GL_TEXTURE_2D,6,0,0,32,32,GL_COMPRESSED_RGB_S3TC_DXT1_EXT,512,0000000014A4A880) glGetError() = GL_INVALID_OPERATION
glGetError()=GL_INVALID_OPERATION 
glCompressedTextureSubImage2DEXT(1,GL_TEXTURE_2D,7,0,0,16,16,GL_COMPRESSED_RGB_S3TC_DXT1_EXT,128,0000000014A4AA80) glGetError() = GL_INVALID_OPERATION
glGetError()=GL_INVALID_OPERATION 
glCompressedTextureSubImage2DEXT(1,GL_TEXTURE_2D,8,0,0,8,8,GL_COMPRESSED_RGB_S3TC_DXT1_EXT,32,0000000014A4AB00) glGetError() = GL_INVALID_OPERATION
glGetError()=GL_INVALID_OPERATION 
glCompressedTextureSubImage2DEXT(1,GL_TEXTURE_2D,9,0,0,4,4,GL_COMPRESSED_RGB_S3TC_DXT1_EXT,8,0000000014A4AB20) glGetError() = GL_INVALID_OPERATION
glGetError()=GL_INVALID_OPERATION 
glCompressedTextureSubImage2DEXT(1,GL_TEXTURE_2D,10,0,0,2,2,GL_COMPRESSED_RGB_S3TC_DXT1_EXT,8,0000000014A4AB28) glGetError() = GL_INVALID_OPERATION
glGetError()=GL_INVALID_OPERATION 
glCompressedTextureSubImage2DEXT(1,GL_TEXTURE_2D,11,0,0,1,1,GL_COMPRESSED_RGB_S3TC_DXT1_EXT,8,0000000014A4AB30) glGetError() = GL_INVALID_OPERATION
glGetError()=GL_INVALID_OPERATION 
glTextureParameteriEXT(1,GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,9729)
glTextureParameteriEXT(1,GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,9729)

1 个答案:

答案 0 :(得分:4)

当您致电glTextureStorage2DEXT时,您只为单个mipmap分配足够的内存。第三个参数是您要分配的mipmap级别数 - 在您的情况下,应该是11:

glTextureStorage2DEXT(1,GL_TEXTURE_2D,11,GL_COMPRESSED_RGB_S3TC_DXT1_EXT,2048,2048)

对于给定的宽度和高度,您可以计算使用floor(log2(max(width, height))) + 1生成完整mipmap链所需的级别数。


抱歉,我没有回答这个问题!您应该拨打setMipLevels,而不是致电setMipLevelRange。正如setMipLevels的Qt文档中所述:

  

对于支持mipmap的纹理目标,此函数设置所请求的mipmap级别数,以便为其分配存储空间。在为纹理分配存储空间之前,应调用此函数。

这应该有效:

mTexture.setMipLevels(floor(log2(max(width, height))) + 1);

或使用parser.getMipmapCount()方法:

mTexture.setMipLevels(parser.getMipmapCount());