Skip to content

Commit

Permalink
Generate mipmap pyramid manually for GL_RGB textures if glGenerateMip…
Browse files Browse the repository at this point in the history
…map() fails

This failure can happen in GLES2 mode when GL_SRGB8 is not color renderable.
  • Loading branch information
10110111 committed Apr 6, 2023
1 parent 00e8d01 commit 4da2958
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/core/StelTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,25 @@ bool StelTexture::glLoad(const GLData& data)
{
glSize = glSize + glSize/3; //mipmaps require 1/3 more mem
}

StelOpenGL::checkGLErrors(__FILE__, __LINE__);
gl->glGenerateMipmap(GL_TEXTURE_2D);

const bool manualMipmapGeneration = gl->glGetError() != GL_NO_ERROR;
if(manualMipmapGeneration && data.format==GL_RGB)
{
// Formula from the glspec, "Mipmapping" subsection in section 3.8.11 Texture Minification
const int totalMipmapLevels = 1+std::floor(std::log2(std::max(width,height)));
qDebug().nospace() << "Making a mipmap pyramid for a " << width << "×" << height << " RGB texture";
QImage img(reinterpret_cast<const uchar*>(data.data.constData()), width, height, QImage::Format_RGB888);
for(int level=1; level<totalMipmapLevels; ++level)
{
img = img.scaled(std::max(1, img.width()/2), std::max(1, img.height()/2),
Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
gl->glTexImage2D(GL_TEXTURE_2D, level, internalFormat, img.width(), img.height(), 0,
static_cast<GLenum>(data.format), static_cast<GLenum>(data.type), img.bits());
}
}
}

//register ID with textureMgr and increment size
Expand Down

0 comments on commit 4da2958

Please sign in to comment.