OpenGL RGB DXT1压缩纹理mipmap上传

问题描述 投票:1回答:1

我正在尝试使用PBO上传GL_COMPRESSED_RGB_S3TC_DXT1_EXT格式的纹理贴图级别。该程序正在使用stb_resize.hstb_dxt.h库调整大小和压缩图像。

显然,图像压缩还可以,但是上传任何mipmap级别会产生以下结果:Broken Mipmaped texture render

但是,如果我没有为纹理定义mipmap级别,则它将正确渲染。另外,由于启用了GL_KHR_debug,我也没有从OpenGL中收到any错误。

这里是用于上传mipmap的opengl代码,介于两者之间的是我自己的一些库代码:

// Load image:
auto img = loadImage2D("parrot.png");

// Resize the image to power-of-two
int max_mipmaps = 1;
unsigned int max_size = nextPow2(std::max(img.width, img.height));
for(int i = max_size; i > 4; i >>= 1)
    max_mipmaps++;

auto rimg = img.scaleCubic(max_size,max_size);

// Allocate PBO to source compressed mipmap levels from
handle_t pbo;
size_t pbosize = rimg.compressed_size();
glCreateBuffers(1, &pbo);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
glBufferStorage(GL_PIXEL_UNPACK_BUFFER, pbosize, 0,
    GL_DYNAMIC_STORAGE_BIT|GL_MAP_WRITE_BIT|GL_MAP_READ_BIT|GL_MAP_PERSISTENT_BIT);
// Map the buffer
void * pbodata = (char*)glMapBufferRange(
    GL_PIXEL_UNPACK_BUFFER, 0, pbosize,
    GL_MAP_WRITE_BIT|GL_MAP_READ_BIT
    |GL_MAP_PERSISTENT_BIT
    |GL_MAP_FLUSH_EXPLICIT_BIT);

// Compress the image and write the data directly into PBO.
// compress_to() fmt returns GL_COMPRESSED_RGB_S3TC_DXT1_EXT
option_t fmt;
rimg.compress_to(pbodata, fmt);
glFlushMappedBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, pbosize);

// Allocate the texture
glCreateTextures(GL_TEXTURE_2D, 1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTextureStorage2D(texture, max_mipmaps, fmt, rimg.width, rimg.width);
// Upload base image level.
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glCompressedTextureSubImage2D(texture, 0, 0,0, rimg.width, rimg.height, fmt, rimg.compressed_size(), 0);
// Process and Upload mipmap levels.
unsigned int mipmapsize = max_size >> 1;
for(int i = 1; i < max_mipmaps && mipmapsize >= 4; ++i) {
    // Resize next mipmap level.
    rimg = img.scaleLinear(mipmapsize,mipmapsize);

    // Compress the image and write result to *pbodata
    rimg.compress_to(pbodata, fmt);
    glFlushMappedBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, pbosize);

    // Upload mipmap image level.
    glCompressedTexSubImage2D(GL_TEXTURE_2D, i, 0,0, rimg.width, rimg.height, fmt, rimg.compressed_size(), 0);

    mipmapsize >>= 1;
}
// Discard the PBO
glUnmapNamedBuffer(pbo);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER,0);
glDeleteBuffers(1, &pbo);

// Set texture params.
glTextureParameteri(texture, GL_TEXTURE_BASE_LEVEL, 0);
glTextureParameteri(texture, GL_TEXTURE_MAX_LEVEL, max_mipmaps - 1);
glTextureParameteri(texture, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTextureParameteri(texture, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
c++ opengl compression textures mipmaps
1个回答
0
投票

执行glFlushMappedBufferRange不是足以确保与GPU的正确同步。这似乎正在发生:整个mipmap金字塔在CPU上创建,之前 GPU处理第一个glCompressedTexSubImage2D调用。

作为对此假设的快速检验,您可以在循环中添加glFinish。要正确同步它,可以使用GL Sync Objects,但最好是根本不需要同步:只使用一个PBO,它要足以容纳整个mipmap金字塔数据。

© www.soinside.com 2019 - 2024. All rights reserved.