Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qsgcompressedtexture.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 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
4
6#include <QDebug>
7#include <QtQuick/private/qquickwindow_p.h>
8#include <QtQuick/private/qquickitem_p.h>
9#include <rhi/qrhi.h>
10
12
13Q_LOGGING_CATEGORY(QSG_LOG_TEXTUREIO, "qt.scenegraph.textureio");
14
22
27
29{
30 if (m_texture)
31 return qint64(m_texture);
32
33 // two textures (and so materials) with not-yet-created texture underneath are never equal
34 return qint64(this);
35}
36
41
43{
44 return m_hasAlpha;
45}
46
48{
49 return false;
50}
51
53 // Copied from QOpenGLTexture.
54 // TODO: make a general solution that can be shared with QtQuick3D (QTBUG-82431)
55 enum {
56 NoFormat = 0, // GL_NONE
57
58 // Unsigned normalized formats
59 R8_UNorm = 0x8229, // GL_R8
60 RG8_UNorm = 0x822B, // GL_RG8
61 RGB8_UNorm = 0x8051, // GL_RGB8
62 RGBA8_UNorm = 0x8058, // GL_RGBA8
63
64 R16_UNorm = 0x822A, // GL_R16
65 RG16_UNorm = 0x822C, // GL_RG16
66 RGB16_UNorm = 0x8054, // GL_RGB16
67 RGBA16_UNorm = 0x805B, // GL_RGBA16
68
69 // Signed normalized formats
70 R8_SNorm = 0x8F94, // GL_R8_SNORM
71 RG8_SNorm = 0x8F95, // GL_RG8_SNORM
72 RGB8_SNorm = 0x8F96, // GL_RGB8_SNORM
73 RGBA8_SNorm = 0x8F97, // GL_RGBA8_SNORM
74
75 R16_SNorm = 0x8F98, // GL_R16_SNORM
76 RG16_SNorm = 0x8F99, // GL_RG16_SNORM
77 RGB16_SNorm = 0x8F9A, // GL_RGB16_SNORM
78 RGBA16_SNorm = 0x8F9B, // GL_RGBA16_SNORM
79
80 // Unsigned integer formats
81 R8U = 0x8232, // GL_R8UI
82 RG8U = 0x8238, // GL_RG8UI
83 RGB8U = 0x8D7D, // GL_RGB8UI
84 RGBA8U = 0x8D7C, // GL_RGBA8UI
85
86 R16U = 0x8234, // GL_R16UI
87 RG16U = 0x823A, // GL_RG16UI
88 RGB16U = 0x8D77, // GL_RGB16UI
89 RGBA16U = 0x8D76, // GL_RGBA16UI
90
91 R32U = 0x8236, // GL_R32UI
92 RG32U = 0x823C, // GL_RG32UI
93 RGB32U = 0x8D71, // GL_RGB32UI
94 RGBA32U = 0x8D70, // GL_RGBA32UI
95
96 // Signed integer formats
97 R8I = 0x8231, // GL_R8I
98 RG8I = 0x8237, // GL_RG8I
99 RGB8I = 0x8D8F, // GL_RGB8I
100 RGBA8I = 0x8D8E, // GL_RGBA8I
101
102 R16I = 0x8233, // GL_R16I
103 RG16I = 0x8239, // GL_RG16I
104 RGB16I = 0x8D89, // GL_RGB16I
105 RGBA16I = 0x8D88, // GL_RGBA16I
106
107 R32I = 0x8235, // GL_R32I
108 RG32I = 0x823B, // GL_RG32I
109 RGB32I = 0x8D83, // GL_RGB32I
110 RGBA32I = 0x8D82, // GL_RGBA32I
111
112 // Floating point formats
113 R16F = 0x822D, // GL_R16F
114 RG16F = 0x822F, // GL_RG16F
115 RGB16F = 0x881B, // GL_RGB16F
116 RGBA16F = 0x881A, // GL_RGBA16F
117
118 R32F = 0x822E, // GL_R32F
119 RG32F = 0x8230, // GL_RG32F
120 RGB32F = 0x8815, // GL_RGB32F
121 RGBA32F = 0x8814, // GL_RGBA32F
122
123 // Packed formats
124 RGB9E5 = 0x8C3D, // GL_RGB9_E5
125 RG11B10F = 0x8C3A, // GL_R11F_G11F_B10F
126 RG3B2 = 0x2A10, // GL_R3_G3_B2
127 R5G6B5 = 0x8D62, // GL_RGB565
128 RGB5A1 = 0x8057, // GL_RGB5_A1
129 RGBA4 = 0x8056, // GL_RGBA4
130 RGB10A2 = 0x906F, // GL_RGB10_A2UI
131
132 // Depth formats
133 D16 = 0x81A5, // GL_DEPTH_COMPONENT16
134 D24 = 0x81A6, // GL_DEPTH_COMPONENT24
135 D24S8 = 0x88F0, // GL_DEPTH24_STENCIL8
136 D32 = 0x81A7, // GL_DEPTH_COMPONENT32
137 D32F = 0x8CAC, // GL_DEPTH_COMPONENT32F
138 D32FS8X24 = 0x8CAD, // GL_DEPTH32F_STENCIL8
139 S8 = 0x8D48, // GL_STENCIL_INDEX8
140
141 // Compressed formats
142 RGB_DXT1 = 0x83F0, // GL_COMPRESSED_RGB_S3TC_DXT1_EXT
143 RGBA_DXT1 = 0x83F1, // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
144 RGBA_DXT3 = 0x83F2, // GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
145 RGBA_DXT5 = 0x83F3, // GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
146 R_ATI1N_UNorm = 0x8DBB, // GL_COMPRESSED_RED_RGTC1
147 R_ATI1N_SNorm = 0x8DBC, // GL_COMPRESSED_SIGNED_RED_RGTC1
148 RG_ATI2N_UNorm = 0x8DBD, // GL_COMPRESSED_RG_RGTC2
149 RG_ATI2N_SNorm = 0x8DBE, // GL_COMPRESSED_SIGNED_RG_RGTC2
150 RGB_BP_UNSIGNED_FLOAT = 0x8E8F, // GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB
151 RGB_BP_SIGNED_FLOAT = 0x8E8E, // GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB
152 RGB_BP_UNorm = 0x8E8C, // GL_COMPRESSED_RGBA_BPTC_UNORM_ARB
153 R11_EAC_UNorm = 0x9270, // GL_COMPRESSED_R11_EAC
154 R11_EAC_SNorm = 0x9271, // GL_COMPRESSED_SIGNED_R11_EAC
155 RG11_EAC_UNorm = 0x9272, // GL_COMPRESSED_RG11_EAC
156 RG11_EAC_SNorm = 0x9273, // GL_COMPRESSED_SIGNED_RG11_EAC
157 RGB8_ETC2 = 0x9274, // GL_COMPRESSED_RGB8_ETC2
158 SRGB8_ETC2 = 0x9275, // GL_COMPRESSED_SRGB8_ETC2
159 RGB8_PunchThrough_Alpha1_ETC2 = 0x9276, // GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2
160 SRGB8_PunchThrough_Alpha1_ETC2 = 0x9277, // GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2
161 RGBA8_ETC2_EAC = 0x9278, // GL_COMPRESSED_RGBA8_ETC2_EAC
162 SRGB8_Alpha8_ETC2_EAC = 0x9279, // GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC
163 RGB8_ETC1 = 0x8D64, // GL_ETC1_RGB8_OES
164 RGBA_ASTC_4x4 = 0x93B0, // GL_COMPRESSED_RGBA_ASTC_4x4_KHR
165 RGBA_ASTC_5x4 = 0x93B1, // GL_COMPRESSED_RGBA_ASTC_5x4_KHR
166 RGBA_ASTC_5x5 = 0x93B2, // GL_COMPRESSED_RGBA_ASTC_5x5_KHR
167 RGBA_ASTC_6x5 = 0x93B3, // GL_COMPRESSED_RGBA_ASTC_6x5_KHR
168 RGBA_ASTC_6x6 = 0x93B4, // GL_COMPRESSED_RGBA_ASTC_6x6_KHR
169 RGBA_ASTC_8x5 = 0x93B5, // GL_COMPRESSED_RGBA_ASTC_8x5_KHR
170 RGBA_ASTC_8x6 = 0x93B6, // GL_COMPRESSED_RGBA_ASTC_8x6_KHR
171 RGBA_ASTC_8x8 = 0x93B7, // GL_COMPRESSED_RGBA_ASTC_8x8_KHR
172 RGBA_ASTC_10x5 = 0x93B8, // GL_COMPRESSED_RGBA_ASTC_10x5_KHR
173 RGBA_ASTC_10x6 = 0x93B9, // GL_COMPRESSED_RGBA_ASTC_10x6_KHR
174 RGBA_ASTC_10x8 = 0x93BA, // GL_COMPRESSED_RGBA_ASTC_10x8_KHR
175 RGBA_ASTC_10x10 = 0x93BB, // GL_COMPRESSED_RGBA_ASTC_10x10_KHR
176 RGBA_ASTC_12x10 = 0x93BC, // GL_COMPRESSED_RGBA_ASTC_12x10_KHR
177 RGBA_ASTC_12x12 = 0x93BD, // GL_COMPRESSED_RGBA_ASTC_12x12_KHR
178 SRGB8_Alpha8_ASTC_4x4 = 0x93D0, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR
179 SRGB8_Alpha8_ASTC_5x4 = 0x93D1, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR
180 SRGB8_Alpha8_ASTC_5x5 = 0x93D2, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR
181 SRGB8_Alpha8_ASTC_6x5 = 0x93D3, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR
182 SRGB8_Alpha8_ASTC_6x6 = 0x93D4, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR
183 SRGB8_Alpha8_ASTC_8x5 = 0x93D5, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR
184 SRGB8_Alpha8_ASTC_8x6 = 0x93D6, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR
185 SRGB8_Alpha8_ASTC_8x8 = 0x93D7, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR
186 SRGB8_Alpha8_ASTC_10x5 = 0x93D8, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR
187 SRGB8_Alpha8_ASTC_10x6 = 0x93D9, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR
188 SRGB8_Alpha8_ASTC_10x8 = 0x93DA, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR
189 SRGB8_Alpha8_ASTC_10x10 = 0x93DB, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR
190 SRGB8_Alpha8_ASTC_12x10 = 0x93DC, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR
191 SRGB8_Alpha8_ASTC_12x12 = 0x93DD, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR
192
193 // sRGB formats
194 SRGB8 = 0x8C41, // GL_SRGB8
195 SRGB8_Alpha8 = 0x8C43, // GL_SRGB8_ALPHA8
196 SRGB_DXT1 = 0x8C4C, // GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
197 SRGB_Alpha_DXT1 = 0x8C4D, // GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
198 SRGB_Alpha_DXT3 = 0x8C4E, // GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
199 SRGB_Alpha_DXT5 = 0x8C4F, // GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
200 SRGB_BP_UNorm = 0x8E8D, // GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB
201
202 // ES 2 formats
203 DepthFormat = 0x1902, // GL_DEPTH_COMPONENT
204 AlphaFormat = 0x1906, // GL_ALPHA
205 RGBFormat = 0x1907, // GL_RGB
206 RGBAFormat = 0x1908, // GL_RGBA
207 LuminanceFormat = 0x1909, // GL_LUMINANCE
208 LuminanceAlphaFormat = 0x190A
209
210 };
211}
212
214{
215 switch (glTextureFormat) {
217 return { QRhiTexture::BC1, false };
219 return { QRhiTexture::BC1, true };
220
222 return { QRhiTexture::BC2, false };
224 return { QRhiTexture::BC2, true };
225
227 return { QRhiTexture::BC3, false };
229 return { QRhiTexture::BC3, true };
230
232 return { QRhiTexture::ETC2_RGB8, false };
234 return { QRhiTexture::ETC2_RGB8, true };
235
237 return { QRhiTexture::ETC2_RGB8A1, false };
239 return { QRhiTexture::ETC2_RGB8A1, true };
240
242 return { QRhiTexture::ETC2_RGBA8, false };
244 return { QRhiTexture::ETC2_RGBA8, true };
245
247 return { QRhiTexture::ASTC_4x4, false };
249 return { QRhiTexture::ASTC_4x4, true };
250
252 return { QRhiTexture::ASTC_5x4, false };
254 return { QRhiTexture::ASTC_5x4, true };
255
257 return { QRhiTexture::ASTC_5x5, false };
259 return { QRhiTexture::ASTC_5x5, true };
260
262 return { QRhiTexture::ASTC_6x5, false };
264 return { QRhiTexture::ASTC_6x5, true };
265
267 return { QRhiTexture::ASTC_6x6, false };
269 return { QRhiTexture::ASTC_6x6, true };
270
272 return { QRhiTexture::ASTC_8x5, false };
274 return { QRhiTexture::ASTC_8x5, true };
275
277 return { QRhiTexture::ASTC_8x6, false };
279 return { QRhiTexture::ASTC_8x6, true };
280
282 return { QRhiTexture::ASTC_8x8, false };
284 return { QRhiTexture::ASTC_8x8, true };
285
287 return { QRhiTexture::ASTC_10x5, false };
289 return { QRhiTexture::ASTC_10x5, true };
290
292 return { QRhiTexture::ASTC_10x6, false };
294 return { QRhiTexture::ASTC_10x6, true };
295
297 return { QRhiTexture::ASTC_10x8, false };
299 return { QRhiTexture::ASTC_10x8, true };
300
302 return { QRhiTexture::ASTC_10x10, false };
304 return { QRhiTexture::ASTC_10x10, true };
305
307 return { QRhiTexture::ASTC_12x10, false };
309 return { QRhiTexture::ASTC_12x10, true };
310
312 return { QRhiTexture::ASTC_12x12, false };
314 return { QRhiTexture::ASTC_12x12, true };
315
316 default:
317 return { QRhiTexture::UnknownFormat, false };
318 }
319}
320
325
327{
328 if (m_uploaded)
329 return;
330
331 m_uploaded = true; // even if fails, no point in trying again
332
333 if (!m_textureData.isValid()) {
334 qCDebug(QSG_LOG_TEXTUREIO, "Invalid texture data for %s", m_textureData.logName().constData());
335 return;
336 }
337
339 if (fmt.rhiFormat == QRhiTexture::UnknownFormat) {
340 qWarning("Unknown compressed format 0x%x", m_textureData.glInternalFormat());
341 return;
342 }
343
344 if (!m_texture) {
345 QRhiTexture::Flags texFlags;
346 if (fmt.isSRGB)
347 texFlags |= QRhiTexture::sRGB;
348
349 if (!rhi->isTextureFormatSupported(fmt.rhiFormat, texFlags)) {
350 qCDebug(QSG_LOG_TEXTUREIO, "Compressed texture format possibly unsupported: 0x%x",
352 // For the Metal backend, don't even try to create an unsupported texture
353 // since trying to do so is invalid.
354 if (rhi->backend() == QRhi::Metal) {
355 qWarning("Unsupported compressed texture format 0x%x", m_textureData.glInternalFormat());
356 return;
357 }
358 }
359
360 m_texture = rhi->newTexture(fmt.rhiFormat, m_size, 1, texFlags);
361 if (!m_texture->create()) {
362 qWarning("Failed to create QRhiTexture for compressed data with format 0x%x",
364 delete m_texture;
365 m_texture = nullptr;
366 return;
367 }
368 }
369
370 // only upload mip level 0 since we never do mipmapping for compressed textures (for now?)
371 resourceUpdates->uploadTexture(
372 m_texture,
376
377 m_textureData = QTextureFileData(); // Release this memory, not needed anymore
378}
379
384
409
411 : m_textureData(texData)
412{
413}
414
416{
417 if (!m_textureData.isValid())
418 return nullptr;
419
420 // attempt to atlas the texture
422 QSGTexture *t = context->compressedTextureForFactory(this);
423 if (t)
424 return t;
425
427}
428
433
438
440
441#include "moc_qsgcompressedtexture_p.cpp"
QByteArray toByteArray() const
Definition qbytearray.h:796
constexpr qsizetype size() const noexcept
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:124
static QQuickWindowPrivate * get(QQuickWindow *c)
\qmltype Window \instantiates QQuickWindow \inqmlmodule QtQuick
\inmodule QtGui
Definition qrhi.h:1731
\inmodule QtGui
Definition qrhi.h:693
\inmodule QtGui
Definition qrhi.h:895
virtual bool create()=0
Creates the corresponding native graphics resources.
@ ASTC_10x8
Definition qrhi.h:959
@ ASTC_12x12
Definition qrhi.h:962
@ ASTC_8x5
Definition qrhi.h:954
@ ASTC_10x5
Definition qrhi.h:957
@ ETC2_RGBA8
Definition qrhi.h:947
@ ASTC_5x5
Definition qrhi.h:951
@ ASTC_4x4
Definition qrhi.h:949
@ ASTC_6x6
Definition qrhi.h:953
@ ASTC_12x10
Definition qrhi.h:961
@ ETC2_RGB8
Definition qrhi.h:945
@ ASTC_5x4
Definition qrhi.h:950
@ ASTC_6x5
Definition qrhi.h:952
@ ASTC_8x8
Definition qrhi.h:956
@ ASTC_10x6
Definition qrhi.h:958
@ ASTC_10x10
Definition qrhi.h:960
@ UnknownFormat
Definition qrhi.h:915
@ ETC2_RGB8A1
Definition qrhi.h:946
@ ASTC_8x6
Definition qrhi.h:955
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
Definition qrhi.h:1804
bool isTextureFormatSupported(QRhiTexture::Format format, QRhiTexture::Flags flags={}) const
Definition qrhi.cpp:10102
Implementation backend() const
Definition qrhi.cpp:8651
@ Metal
Definition qrhi.h:1811
QRhiTexture * newTexture(QRhiTexture::Format format, const QSize &pixelSize, int sampleCount=1, QRhiTexture::Flags flags={})
Definition qrhi.cpp:10562
QSGCompressedTextureFactory(const QTextureFileData &texData)
int textureByteCount() const override
Returns the number of bytes of memory the texture consumes.
QSize textureSize() const override
Returns the size of the texture.
QSGTexture * createTexture(QQuickWindow *) const override
This function is called on the scene graph rendering thread to create a QSGTexture instance from the ...
QSGCompressedTexture(const QTextureFileData &texData)
void commitTextureOperations(QRhi *rhi, QRhiResourceUpdateBatch *resourceUpdates) override
Call this function to enqueue image upload operations to resourceUpdates, in case there are any pendi...
QRhiTexture * rhiTexture() const override
static FormatInfo formatInfo(quint32 glTextureFormat)
static bool formatIsOpaque(quint32 glTextureFormat)
QTextureFileData textureData() const
bool hasAlphaChannel() const override
Returns true if the texture data contains an alpha channel.
bool hasMipmaps() const override
Returns true if the texture data contains mipmap levels.
qint64 comparisonKey() const override
Returns a key suitable for comparing textures.
QSize textureSize() const override
Returns the size of the texture in pixels.
\inmodule QtQuick
Definition qsgtexture.h:20
\inmodule QtCore
Definition qsize.h:25
quint32 glInternalFormat() const
QByteArray logName() const
QByteArrayView getDataView(int level=0, int face=0) const
#define this
Definition dialogs.cpp:9
Combined button and popup list for selecting options.
static void * context
#define qWarning
Definition qlogging.h:166
#define Q_LOGGING_CATEGORY(name,...)
#define qCDebug(category,...)
GLdouble GLdouble t
Definition qopenglext.h:243
unsigned int quint32
Definition qtypes.h:50
long long qint64
Definition qtypes.h:60
QVideoFrameFormat::PixelFormat fmt
aWidget window() -> setWindowTitle("New Window Title")
[2]