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; uploadFormat = QImage::Format_RGBA8888_Premultiplied;
} }
} }
bool useUnpack = image.format() == uploadFormat && !src.isNull();
QImage im; QImage im = image;
if (useUnpack) { if (im.format() != uploadFormat) {
im = image; im.convertTo(uploadFormat);
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);
}
if (im.format() != uploadFormat) {
im.convertTo(uploadFormat);
}
} }
int width = image.width(); QRect rect = src;
int height = image.height(); if (rect.isEmpty()) {
if (!src.isNull()) { rect = im.rect();
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(); 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(); unbind();
if (useUnpack) { glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0); glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
}
} }
void GLTexture::bind() void GLTexture::bind()