From 04044dd76d32c973bda29aa5362d9318908b49c4 Mon Sep 17 00:00:00 2001 From: Josh Dowell Date: Thu, 14 Sep 2023 06:18:51 +0100 Subject: [PATCH] lightpreview: fix texture mips not being generated (#385) This was happening because allocateStorage allocates space for the texture and its mips up-front, the user of QOpenGLTexture is required to determine the number of mips required for a texture beforehand. allocateStorage was allocating space for mip 0 and no others, leading to no additional mips being generated. This also adds explicit format and type to allocateStorage for only this instance, although it still remains optional. --- lightpreview/glview.cpp | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/lightpreview/glview.cpp b/lightpreview/glview.cpp index c7ac6a1e..df41e69e 100644 --- a/lightpreview/glview.cpp +++ b/lightpreview/glview.cpp @@ -42,6 +42,13 @@ See file, 'COPYING', for details. #include #include +// given a width and height, returns the number of mips required +// see https://registry.khronos.org/OpenGL/extensions/ARB/ARB_texture_non_power_of_two.txt +static int GetMipLevelsForDimensions(int w, int h) +{ + return 1 + static_cast(std::floor(std::log2(std::max(w, h)))); +} + GLView::GLView(QWidget *parent) : QOpenGLWidget(parent), m_keysPressed(0), @@ -1289,19 +1296,20 @@ void GLView::renderBSP(const QString &file, const mbsp_t &bsp, const bspxentries if (!qtexture) { qtexture = std::make_shared(QOpenGLTexture::Target2D); - qtexture->setSize(texture->width, texture->height); - qtexture->setFormat(QOpenGLTexture::TextureFormat::RGBA8_UNorm); + int mipLevels = GetMipLevelsForDimensions(texture->width, texture->height); - qtexture->allocateStorage(); + qtexture->setFormat(QOpenGLTexture::TextureFormat::RGBA8_UNorm); + qtexture->setSize(texture->width, texture->height); + qtexture->setMipLevels(mipLevels); + + qtexture->allocateStorage(QOpenGLTexture::RGBA, QOpenGLTexture::UInt8); + + qtexture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear); + qtexture->setMagnificationFilter(m_filter); + qtexture->setMaximumAnisotropy(16); qtexture->setData( QOpenGLTexture::RGBA, QOpenGLTexture::UInt8, reinterpret_cast(texture->pixels.data())); - - qtexture->setMaximumAnisotropy(16); - qtexture->setAutoMipMapGenerationEnabled(true); - - qtexture->setMagnificationFilter(m_filter); - qtexture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear); } const size_t dc_index_count = indexBuffer.size() - dc_first_index;