Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qopengltextureuploader.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
7#include <qimage.h>
8#include <qmath.h>
9#include <qopenglfunctions.h>
10#include <private/qopenglcontext_p.h>
11#include <private/qopenglextensions_p.h>
12
13#ifndef GL_HALF_FLOAT
14#define GL_HALF_FLOAT 0x140B
15#endif
16
17#ifndef GL_RED
18#define GL_RED 0x1903
19#endif
20
21#ifndef GL_GREEN
22#define GL_GREEN 0x1904
23#endif
24
25#ifndef GL_BLUE
26#define GL_BLUE 0x1905
27#endif
28
29#ifndef GL_RGB10_A2
30#define GL_RGB10_A2 0x8059
31#endif
32
33#ifndef GL_RGBA16
34#define GL_RGBA16 0x805B
35#endif
36
37#ifndef GL_BGR
38#define GL_BGR 0x80E0
39#endif
40
41#ifndef GL_BGRA
42#define GL_BGRA 0x80E1
43#endif
44
45#ifndef GL_UNSIGNED_INT_8_8_8_8_REV
46#define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
47#endif
48
49#ifndef GL_UNSIGNED_INT_2_10_10_10_REV
50#define GL_UNSIGNED_INT_2_10_10_10_REV 0x8368
51#endif
52
53#ifndef GL_RGBA16F
54#define GL_RGBA16F 0x881A
55#endif
56
57#ifndef GL_RGBA32F
58#define GL_RGBA32F 0x8814
59#endif
60
61#ifndef GL_TEXTURE_SWIZZLE_R
62#define GL_TEXTURE_SWIZZLE_R 0x8E42
63#endif
64
65#ifndef GL_TEXTURE_SWIZZLE_G
66#define GL_TEXTURE_SWIZZLE_G 0x8E43
67#endif
68
69#ifndef GL_TEXTURE_SWIZZLE_B
70#define GL_TEXTURE_SWIZZLE_B 0x8E44
71#endif
72
73#ifndef GL_TEXTURE_SWIZZLE_A
74#define GL_TEXTURE_SWIZZLE_A 0x8E45
75#endif
76
77#ifndef GL_SRGB
78#define GL_SRGB 0x8C40
79#endif
80#ifndef GL_SRGB_ALPHA
81#define GL_SRGB_ALPHA 0x8C42
82#endif
83
85
86qsizetype QOpenGLTextureUploader::textureImage(GLenum target, const QImage &image, QOpenGLTextureUploader::BindOptions options, QSize maxSize)
87{
88 QOpenGLContext *context = QOpenGLContext::currentContext();
89 QOpenGLExtensions *funcs = static_cast<QOpenGLExtensions*>(context->functions());
90
91 QImage tx;
92 GLenum externalFormat;
93 GLenum internalFormat;
94 GLuint pixelType;
95 QImage::Format targetFormat = QImage::Format_Invalid;
96 const bool isOpenGL12orBetter = !context->isOpenGLES() && (context->format().majorVersion() >= 2 || context->format().minorVersion() >= 2);
97 const bool isOpenGLES3orBetter = context->isOpenGLES() && context->format().majorVersion() >= 3;
98 const bool sRgbBinding = (options & SRgbBindOption);
99 Q_ASSERT(isOpenGL12orBetter || context->isOpenGLES());
100 Q_ASSERT((options & (SRgbBindOption | UseRedForAlphaAndLuminanceBindOption)) != (SRgbBindOption | UseRedForAlphaAndLuminanceBindOption));
101
102 switch (image.format()) {
103 case QImage::Format_RGB32:
104 case QImage::Format_ARGB32:
105 case QImage::Format_ARGB32_Premultiplied:
106 if (isOpenGL12orBetter) {
107 externalFormat = GL_BGRA;
108 internalFormat = GL_RGBA;
109 pixelType = GL_UNSIGNED_INT_8_8_8_8_REV;
110#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
111 // Without GL_UNSIGNED_INT_8_8_8_8_REV, BGRA only matches ARGB on little endian:
112 } else if (funcs->hasOpenGLExtension(QOpenGLExtensions::BGRATextureFormat) && !sRgbBinding) {
113 // The GL_EXT_texture_format_BGRA8888 extension requires the internal format to match the external.
114 externalFormat = internalFormat = GL_BGRA;
115 pixelType = GL_UNSIGNED_BYTE;
116 // ES 3 rejects BGRA data in an RGBA texture unless the driver really
117 // implements the Apple extension, and its core swizzle is safer.
118 } else if (context->isOpenGLES()
119 && !funcs->hasOpenGLExtension(QOpenGLExtensions::TextureSwizzle)
120 && context->hasExtension("GL_APPLE_texture_format_BGRA8888")) {
121 // Is only allowed as an external format like OpenGL.
122 externalFormat = GL_BGRA;
123 internalFormat = GL_RGBA;
124 pixelType = GL_UNSIGNED_BYTE;
125#endif
126 } else if (funcs->hasOpenGLExtension(QOpenGLExtensions::TextureSwizzle)) {
127#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
128 funcs->glTexParameteri(target, GL_TEXTURE_SWIZZLE_R, GL_BLUE);
129 funcs->glTexParameteri(target, GL_TEXTURE_SWIZZLE_B, GL_RED);
130#else
131 funcs->glTexParameteri(target, GL_TEXTURE_SWIZZLE_R, GL_GREEN);
132 funcs->glTexParameteri(target, GL_TEXTURE_SWIZZLE_G, GL_BLUE);
133 funcs->glTexParameteri(target, GL_TEXTURE_SWIZZLE_B, GL_ALPHA);
134 funcs->glTexParameteri(target, GL_TEXTURE_SWIZZLE_A, GL_RED);
135#endif
136 externalFormat = internalFormat = GL_RGBA;
137 pixelType = GL_UNSIGNED_BYTE;
138 } else {
139 // No support for direct ARGB32 upload.
140 break;
141 }
142 targetFormat = image.format();
143 break;
144 case QImage::Format_BGR30:
145 case QImage::Format_A2BGR30_Premultiplied:
146 if (sRgbBinding) {
147 // Always needs conversion
148 break;
149 } else if (isOpenGL12orBetter || isOpenGLES3orBetter) {
151 externalFormat = GL_RGBA;
152 internalFormat = GL_RGB10_A2;
153 targetFormat = image.format();
154 }
155 break;
156 case QImage::Format_RGB30:
157 case QImage::Format_A2RGB30_Premultiplied:
158 if (sRgbBinding) {
159 // Always needs conversion
160 break;
161 } else if (isOpenGL12orBetter) {
163 externalFormat = GL_BGRA;
164 internalFormat = GL_RGB10_A2;
165 targetFormat = image.format();
166 } else if (funcs->hasOpenGLExtension(QOpenGLExtensions::TextureSwizzle)) {
167 funcs->glTexParameteri(target, GL_TEXTURE_SWIZZLE_B, GL_RED);
168 funcs->glTexParameteri(target, GL_TEXTURE_SWIZZLE_R, GL_BLUE);
170 externalFormat = GL_RGBA;
171 internalFormat = GL_RGB10_A2;
172 targetFormat = image.format();
173 }
174 break;
175 case QImage::Format_RGB444:
176 case QImage::Format_RGB555:
177 case QImage::Format_RGB16:
178 if (isOpenGL12orBetter || context->isOpenGLES()) {
179 externalFormat = internalFormat = GL_RGB;
180 pixelType = GL_UNSIGNED_SHORT_5_6_5;
181 targetFormat = QImage::Format_RGB16;
182 }
183 break;
184 case QImage::Format_RGB666:
185 case QImage::Format_RGB888:
186 externalFormat = internalFormat = GL_RGB;
187 pixelType = GL_UNSIGNED_BYTE;
188 targetFormat = QImage::Format_RGB888;
189 break;
190 case QImage::Format_BGR888:
191 if (isOpenGL12orBetter) {
192 externalFormat = GL_BGR;
193 internalFormat = GL_RGB;
194 pixelType = GL_UNSIGNED_BYTE;
195 targetFormat = QImage::Format_BGR888;
196 } else if (funcs->hasOpenGLExtension(QOpenGLExtensions::TextureSwizzle)) {
197 funcs->glTexParameteri(target, GL_TEXTURE_SWIZZLE_B, GL_RED);
198 funcs->glTexParameteri(target, GL_TEXTURE_SWIZZLE_R, GL_BLUE);
199 externalFormat = internalFormat = GL_RGB;
200 pixelType = GL_UNSIGNED_BYTE;
201 targetFormat = QImage::Format_BGR888;
202 }
203 break;
204 case QImage::Format_RGBX8888:
205 case QImage::Format_RGBA8888:
206 case QImage::Format_RGBA8888_Premultiplied:
207 externalFormat = internalFormat = GL_RGBA;
208 pixelType = GL_UNSIGNED_BYTE;
209 targetFormat = image.format();
210 break;
211 case QImage::Format_RGBX64:
212 case QImage::Format_RGBA64:
213 case QImage::Format_RGBA64_Premultiplied:
214 externalFormat = internalFormat = GL_RGBA;
215 if (isOpenGL12orBetter || (context->isOpenGLES() && context->format().majorVersion() >= 3))
216 internalFormat = GL_RGBA16;
217 pixelType = GL_UNSIGNED_SHORT;
218 targetFormat = image.format();
219 break;
220 case QImage::Format_RGBX16FPx4:
221 case QImage::Format_RGBA16FPx4:
222 case QImage::Format_RGBA16FPx4_Premultiplied:
223 if (context->format().majorVersion() >= 3) {
224 externalFormat = GL_RGBA;
225 internalFormat = GL_RGBA16F;
226 pixelType = GL_HALF_FLOAT;
227 targetFormat = image.format();
228 }
229 break;
230 case QImage::Format_RGBX32FPx4:
231 case QImage::Format_RGBA32FPx4:
232 case QImage::Format_RGBA32FPx4_Premultiplied:
233 externalFormat = internalFormat = GL_RGBA;
234 if (context->format().majorVersion() >= 3)
235 internalFormat = GL_RGBA32F;
236 pixelType = GL_FLOAT;
237 targetFormat = image.format();
238 break;
239 case QImage::Format_Indexed8:
240 if (sRgbBinding) {
241 // Always needs conversion
242 break;
243 } else if (options & UseRedForAlphaAndLuminanceBindOption) {
244 externalFormat = internalFormat = GL_RED;
245 pixelType = GL_UNSIGNED_BYTE;
246 targetFormat = image.format();
247 }
248 break;
249 case QImage::Format_Alpha8:
250 if (sRgbBinding) {
251 // Always needs conversion
252 break;
253 } else if (options & UseRedForAlphaAndLuminanceBindOption) {
254 externalFormat = internalFormat = GL_RED;
255 pixelType = GL_UNSIGNED_BYTE;
256 targetFormat = image.format();
257 } else if (context->isOpenGLES() || context->format().profile() != QSurfaceFormat::CoreProfile) {
258 externalFormat = internalFormat = GL_ALPHA;
259 pixelType = GL_UNSIGNED_BYTE;
260 targetFormat = image.format();
261 } else if (funcs->hasOpenGLExtension(QOpenGLExtensions::TextureSwizzle)) {
262 funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_ALPHA);
263 funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_ZERO);
264 funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_ZERO);
265 funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ZERO);
266 externalFormat = internalFormat = GL_RED;
267 pixelType = GL_UNSIGNED_BYTE;
268 targetFormat = image.format();
269 }
270 break;
271 case QImage::Format_Grayscale8:
272 if (sRgbBinding) {
273 // Always needs conversion
274 break;
275 } else if (options & UseRedForAlphaAndLuminanceBindOption) {
276 externalFormat = internalFormat = GL_RED;
277 pixelType = GL_UNSIGNED_BYTE;
278 targetFormat = image.format();
279 } else if (context->isOpenGLES() || context->format().profile() != QSurfaceFormat::CoreProfile) {
280 externalFormat = internalFormat = GL_LUMINANCE;
281 pixelType = GL_UNSIGNED_BYTE;
282 targetFormat = image.format();
283 } else if (funcs->hasOpenGLExtension(QOpenGLExtensions::TextureSwizzle)) {
284 funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED);
285 funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_RED);
286 funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
287 funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ONE);
288 externalFormat = internalFormat = GL_RED;
289 pixelType = GL_UNSIGNED_BYTE;
290 targetFormat = image.format();
291 }
292 break;
293 case QImage::Format_Grayscale16:
294 if (sRgbBinding) {
295 // Always needs conversion
296 break;
297 } else if (options & UseRedForAlphaAndLuminanceBindOption) {
298 externalFormat = internalFormat = GL_RED;
299 pixelType = GL_UNSIGNED_SHORT;
300 targetFormat = image.format();
301 } else if (context->isOpenGLES() || context->format().profile() != QSurfaceFormat::CoreProfile) {
302 externalFormat = internalFormat = GL_LUMINANCE;
303 pixelType = GL_UNSIGNED_SHORT;
304 targetFormat = image.format();
305 } else if (funcs->hasOpenGLExtension(QOpenGLExtensions::TextureSwizzle)) {
306 funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED);
307 funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_RED);
308 funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
309 funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ONE);
310 externalFormat = internalFormat = GL_RED;
311 pixelType = GL_UNSIGNED_SHORT;
312 targetFormat = image.format();
313 }
314 break;
315 default:
316 break;
317 }
318
319 // If no direct upload was detected above, convert to RGBA8888 and upload that
320 if (targetFormat == QImage::Format_Invalid) {
321 externalFormat = internalFormat = GL_RGBA;
322 pixelType = GL_UNSIGNED_BYTE;
323 if (!image.hasAlphaChannel())
324 targetFormat = QImage::Format_RGBX8888;
325 else
326 targetFormat = QImage::Format_RGBA8888;
327 }
328
329 if (options & PremultipliedAlphaBindOption) {
330 if (targetFormat == QImage::Format_ARGB32)
331 targetFormat = QImage::Format_ARGB32_Premultiplied;
332 else if (targetFormat == QImage::Format_RGBA8888)
333 targetFormat = QImage::Format_RGBA8888_Premultiplied;
334 else if (targetFormat == QImage::Format_RGBA64)
335 targetFormat = QImage::Format_RGBA64_Premultiplied;
336 else if (targetFormat == QImage::Format_RGBA16FPx4)
337 targetFormat = QImage::Format_RGBA16FPx4_Premultiplied;
338 else if (targetFormat == QImage::Format_RGBA32FPx4)
339 targetFormat = QImage::Format_RGBA32FPx4_Premultiplied;
340 } else {
341 if (targetFormat == QImage::Format_ARGB32_Premultiplied)
342 targetFormat = QImage::Format_ARGB32;
343 else if (targetFormat == QImage::Format_RGBA8888_Premultiplied)
344 targetFormat = QImage::Format_RGBA8888;
345 else if (targetFormat == QImage::Format_RGBA64_Premultiplied)
346 targetFormat = QImage::Format_RGBA64;
347 else if (targetFormat == QImage::Format_RGBA16FPx4_Premultiplied)
348 targetFormat = QImage::Format_RGBA16FPx4;
349 else if (targetFormat == QImage::Format_RGBA32FPx4_Premultiplied)
350 targetFormat = QImage::Format_RGBA32FPx4;
351 }
352
353 if (sRgbBinding) {
354 Q_ASSERT(internalFormat == GL_RGBA || internalFormat == GL_RGB);
355 if (image.hasAlphaChannel())
356 internalFormat = GL_SRGB_ALPHA;
357 else
358 internalFormat = GL_SRGB;
359 }
360
361 if (image.format() != targetFormat)
362 tx = image.convertToFormat(targetFormat);
363 else
364 tx = image;
365
366 QSize newSize = tx.size();
367 if (!maxSize.isEmpty())
368 newSize = newSize.boundedTo(maxSize);
369 if (options & PowerOfTwoBindOption) {
370 newSize.setWidth(qNextPowerOfTwo(newSize.width() - 1));
371 newSize.setHeight(qNextPowerOfTwo(newSize.height() - 1));
372 }
373
374 if (newSize != tx.size())
375 tx = tx.scaled(newSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
376
377 // Handle cases where the QImage is actually a sub image of its image data:
378 qsizetype naturalBpl = ((qsizetype(tx.width()) * tx.depth() + 31) >> 5) << 2;
379 if (tx.bytesPerLine() != naturalBpl)
380 tx = tx.copy(tx.rect());
381
382 funcs->glTexImage2D(target, 0, internalFormat, tx.width(), tx.height(), 0, externalFormat, pixelType, tx.constBits());
383
384 qsizetype cost = qint64(tx.width()) * tx.height() * tx.depth() / 8;
385
386 return cost;
387}
388
389QT_END_NAMESPACE
Combined button and popup list for selecting options.
#define GL_TEXTURE_SWIZZLE_G
#define GL_BGR
Definition qopenglext.h:96
#define GL_RGBA16F
Definition qopenglext.h:913
#define GL_TEXTURE_SWIZZLE_A
#define GL_BGRA
Definition qopenglext.h:97
#define GL_SRGB_ALPHA
Definition qopenglext.h:864
#define GL_TEXTURE_SWIZZLE_B
#define GL_HALF_FLOAT
#define GL_RGBA32F
Definition qopenglext.h:911
#define GL_SRGB
Definition qopenglext.h:862
#define GL_UNSIGNED_INT_8_8_8_8_REV
Definition qopenglext.h:94
#define GL_TEXTURE_SWIZZLE_R
#define GL_RGBA16
#define GL_BLUE
#define GL_UNSIGNED_INT_2_10_10_10_REV
#define GL_RED