opengl: Simplify GLTexture::update()

Use the unpack code path by default, it lets us to simplify the code and
also fix texture uploading when the image stride is not
"width * bytes per pixel".
This commit is contained in:
Vlad Zahorodnii 2024-08-15 14:48:13 +03:00
parent 98953b0218
commit bfff8ac52a

View file

@ -182,44 +182,31 @@ void GLTexture::update(const QImage &image, const QPoint &offset, const QRect &s
uploadFormat = QImage::Format_RGBA8888_Premultiplied;
}
}
bool useUnpack = image.format() == uploadFormat && !src.isNull();
QImage im;
if (useUnpack) {
im = image;
Q_ASSERT(im.depth() % 8 == 0);
glPixelStorei(GL_UNPACK_ROW_LENGTH, im.bytesPerLine() / (im.depth() / 8));
glPixelStorei(GL_UNPACK_SKIP_PIXELS, src.x());
glPixelStorei(GL_UNPACK_SKIP_ROWS, src.y());
} else {
if (src.isNull()) {
im = image;
} else {
im = image.copy(src);
}
QImage im = image;
if (im.format() != uploadFormat) {
im.convertTo(uploadFormat);
}
QRect rect = src;
if (rect.isEmpty()) {
rect = im.rect();
}
int width = image.width();
int height = image.height();
if (!src.isNull()) {
width = src.width();
height = src.height();
}
Q_ASSERT(im.depth() % 8 == 0);
glPixelStorei(GL_UNPACK_ROW_LENGTH, im.bytesPerLine() / (im.depth() / 8));
glPixelStorei(GL_UNPACK_SKIP_PIXELS, rect.x());
glPixelStorei(GL_UNPACK_SKIP_ROWS, rect.y());
bind();
glTexSubImage2D(d->m_target, 0, offset.x(), offset.y(), width, height, glFormat, type, im.constBits());
glTexSubImage2D(d->m_target, 0, offset.x(), offset.y(), rect.width(), rect.height(), glFormat, type, im.constBits());
unbind();
if (useUnpack) {
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
}
}
void GLTexture::bind()