opengl: Re-arrange code in GLTexture::upload()

Re-arrange the code so it's easier to set GL_UNPACK_ROW_LENGTH.
This commit is contained in:
Vlad Zahorodnii 2024-08-19 12:55:10 +03:00
parent def0bde5e9
commit 31adedba3a

View file

@ -547,53 +547,59 @@ std::unique_ptr<GLTexture> GLTexture::upload(const QImage &image)
qCWarning(KWIN_OPENGL, "generating OpenGL texture handle failed"); qCWarning(KWIN_OPENGL, "generating OpenGL texture handle failed");
return nullptr; return nullptr;
} }
glBindTexture(GL_TEXTURE_2D, texture);
const auto context = OpenGlContext::currentContext(); const auto context = OpenGlContext::currentContext();
GLenum internalFormat; GLenum internalFormat;
if (!context->isOpenGLES()) {
QImage im;
GLenum format; GLenum format;
GLenum type; GLenum type;
QImage::Format uploadFormat;
if (!context->isOpenGLES()) {
const QImage::Format index = image.format(); const QImage::Format index = image.format();
if (index < sizeof(formatTable) / sizeof(formatTable[0]) && formatTable[index].internalFormat if (index < sizeof(formatTable) / sizeof(formatTable[0]) && formatTable[index].internalFormat
&& !(formatTable[index].type == GL_UNSIGNED_SHORT && !context->supports16BitTextures())) { && !(formatTable[index].type == GL_UNSIGNED_SHORT && !context->supports16BitTextures())) {
internalFormat = formatTable[index].internalFormat; internalFormat = formatTable[index].internalFormat;
format = formatTable[index].format; format = formatTable[index].format;
type = formatTable[index].type; type = formatTable[index].type;
im = image; uploadFormat = index;
} else { } else {
im = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
internalFormat = GL_RGBA8; internalFormat = GL_RGBA8;
format = GL_BGRA; format = GL_BGRA;
type = GL_UNSIGNED_INT_8_8_8_8_REV; type = GL_UNSIGNED_INT_8_8_8_8_REV;
uploadFormat = QImage::Format_ARGB32_Premultiplied;
}
} else {
if (context->supportsARGB32Textures()) {
internalFormat = GL_BGRA_EXT;
format = GL_BGRA_EXT;
type = GL_UNSIGNED_BYTE;
uploadFormat = QImage::Format_ARGB32_Premultiplied;
} else {
internalFormat = GL_RGBA;
format = GL_RGBA;
type = GL_UNSIGNED_BYTE;
uploadFormat = QImage::Format_RGBA8888_Premultiplied;
}
} }
QImage im = image;
if (im.format() != uploadFormat) {
im.convertTo(uploadFormat);
}
glBindTexture(GL_TEXTURE_2D, texture);
if (!context->isOpenGLES()) {
if (context->supportsTextureStorage()) { if (context->supportsTextureStorage()) {
glTexStorage2D(GL_TEXTURE_2D, 1, internalFormat, im.width(), im.height()); glTexStorage2D(GL_TEXTURE_2D, 1, internalFormat, im.width(), im.height());
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, im.width(), im.height(), glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, im.width(), im.height(), format, type, im.constBits());
format, type, im.constBits());
} else { } else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, im.width(), im.height(), 0, glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, im.width(), im.height(), 0, format, type, im.constBits());
format, type, im.constBits());
} }
} else { } else {
internalFormat = GL_RGBA8; glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, im.width(), im.height(), 0, format, type, im.constBits());
if (context->supportsARGB32Textures()) {
const QImage im = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, im.width(), im.height(),
0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, im.constBits());
} else {
const QImage im = image.convertToFormat(QImage::Format_RGBA8888_Premultiplied);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, im.width(), im.height(),
0, GL_RGBA, GL_UNSIGNED_BYTE, im.constBits());
}
} }
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
return std::unique_ptr<GLTexture>(new GLTexture(GL_TEXTURE_2D, texture, internalFormat, image.size(), 1, true, OutputTransform::FlipY)); return std::unique_ptr<GLTexture>(new GLTexture(GL_TEXTURE_2D, texture, internalFormat, image.size(), 1, true, OutputTransform::FlipY));
} }