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 | |
5 | #include "qsgcompressedtexture_p.h" |
6 | #include <QDebug> |
7 | #include <QtQuick/private/qquickwindow_p.h> |
8 | #include <QtQuick/private/qquickitem_p.h> |
9 | #include <rhi/qrhi.h> |
10 | |
11 | QT_BEGIN_NAMESPACE |
12 | |
13 | Q_LOGGING_CATEGORY(QSG_LOG_TEXTUREIO, "qt.scenegraph.textureio" ); |
14 | |
15 | QSGCompressedTexture::QSGCompressedTexture(const QTextureFileData &texData) |
16 | : QSGTexture(*(new QSGTexturePrivate(this))), |
17 | m_textureData(texData) |
18 | { |
19 | m_size = m_textureData.size(); |
20 | m_hasAlpha = !formatIsOpaque(glTextureFormat: m_textureData.glInternalFormat()); |
21 | } |
22 | |
23 | QSGCompressedTexture::~QSGCompressedTexture() |
24 | { |
25 | delete m_texture; |
26 | } |
27 | |
28 | qint64 QSGCompressedTexture::comparisonKey() const |
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 | |
37 | QSize QSGCompressedTexture::textureSize() const |
38 | { |
39 | return m_size; |
40 | } |
41 | |
42 | bool QSGCompressedTexture::hasAlphaChannel() const |
43 | { |
44 | return m_hasAlpha; |
45 | } |
46 | |
47 | bool QSGCompressedTexture::hasMipmaps() const |
48 | { |
49 | return false; |
50 | } |
51 | |
52 | namespace QInternalGLTextureFormat { |
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 | |
213 | QSGCompressedTexture::FormatInfo QSGCompressedTexture::formatInfo(quint32 glTextureFormat) |
214 | { |
215 | switch (glTextureFormat) { |
216 | case QInternalGLTextureFormat::RGB_DXT1: |
217 | return { .rhiFormat: QRhiTexture::BC1, .isSRGB: false }; |
218 | case QInternalGLTextureFormat::SRGB_DXT1: |
219 | return { .rhiFormat: QRhiTexture::BC1, .isSRGB: true }; |
220 | |
221 | case QInternalGLTextureFormat::RGBA_DXT3: |
222 | return { .rhiFormat: QRhiTexture::BC2, .isSRGB: false }; |
223 | case QInternalGLTextureFormat::SRGB_Alpha_DXT3: |
224 | return { .rhiFormat: QRhiTexture::BC2, .isSRGB: true }; |
225 | |
226 | case QInternalGLTextureFormat::RGBA_DXT5: |
227 | return { .rhiFormat: QRhiTexture::BC3, .isSRGB: false }; |
228 | case QInternalGLTextureFormat::SRGB_Alpha_DXT5: |
229 | return { .rhiFormat: QRhiTexture::BC3, .isSRGB: true }; |
230 | |
231 | case QInternalGLTextureFormat::RGB8_ETC2: |
232 | return { .rhiFormat: QRhiTexture::ETC2_RGB8, .isSRGB: false }; |
233 | case QInternalGLTextureFormat::SRGB8_ETC2: |
234 | return { .rhiFormat: QRhiTexture::ETC2_RGB8, .isSRGB: true }; |
235 | |
236 | case QInternalGLTextureFormat::RGB8_PunchThrough_Alpha1_ETC2: |
237 | return { .rhiFormat: QRhiTexture::ETC2_RGB8A1, .isSRGB: false }; |
238 | case QInternalGLTextureFormat::SRGB8_PunchThrough_Alpha1_ETC2: |
239 | return { .rhiFormat: QRhiTexture::ETC2_RGB8A1, .isSRGB: true }; |
240 | |
241 | case QInternalGLTextureFormat::RGBA8_ETC2_EAC: |
242 | return { .rhiFormat: QRhiTexture::ETC2_RGBA8, .isSRGB: false }; |
243 | case QInternalGLTextureFormat::SRGB8_Alpha8_ETC2_EAC: |
244 | return { .rhiFormat: QRhiTexture::ETC2_RGBA8, .isSRGB: true }; |
245 | |
246 | case QInternalGLTextureFormat::RGBA_ASTC_4x4: |
247 | return { .rhiFormat: QRhiTexture::ASTC_4x4, .isSRGB: false }; |
248 | case QInternalGLTextureFormat::SRGB8_Alpha8_ASTC_4x4: |
249 | return { .rhiFormat: QRhiTexture::ASTC_4x4, .isSRGB: true }; |
250 | |
251 | case QInternalGLTextureFormat::RGBA_ASTC_5x4: |
252 | return { .rhiFormat: QRhiTexture::ASTC_5x4, .isSRGB: false }; |
253 | case QInternalGLTextureFormat::SRGB8_Alpha8_ASTC_5x4: |
254 | return { .rhiFormat: QRhiTexture::ASTC_5x4, .isSRGB: true }; |
255 | |
256 | case QInternalGLTextureFormat::RGBA_ASTC_5x5: |
257 | return { .rhiFormat: QRhiTexture::ASTC_5x5, .isSRGB: false }; |
258 | case QInternalGLTextureFormat::SRGB8_Alpha8_ASTC_5x5: |
259 | return { .rhiFormat: QRhiTexture::ASTC_5x5, .isSRGB: true }; |
260 | |
261 | case QInternalGLTextureFormat::RGBA_ASTC_6x5: |
262 | return { .rhiFormat: QRhiTexture::ASTC_6x5, .isSRGB: false }; |
263 | case QInternalGLTextureFormat::SRGB8_Alpha8_ASTC_6x5: |
264 | return { .rhiFormat: QRhiTexture::ASTC_6x5, .isSRGB: true }; |
265 | |
266 | case QInternalGLTextureFormat::RGBA_ASTC_6x6: |
267 | return { .rhiFormat: QRhiTexture::ASTC_6x6, .isSRGB: false }; |
268 | case QInternalGLTextureFormat::SRGB8_Alpha8_ASTC_6x6: |
269 | return { .rhiFormat: QRhiTexture::ASTC_6x6, .isSRGB: true }; |
270 | |
271 | case QInternalGLTextureFormat::RGBA_ASTC_8x5: |
272 | return { .rhiFormat: QRhiTexture::ASTC_8x5, .isSRGB: false }; |
273 | case QInternalGLTextureFormat::SRGB8_Alpha8_ASTC_8x5: |
274 | return { .rhiFormat: QRhiTexture::ASTC_8x5, .isSRGB: true }; |
275 | |
276 | case QInternalGLTextureFormat::RGBA_ASTC_8x6: |
277 | return { .rhiFormat: QRhiTexture::ASTC_8x6, .isSRGB: false }; |
278 | case QInternalGLTextureFormat::SRGB8_Alpha8_ASTC_8x6: |
279 | return { .rhiFormat: QRhiTexture::ASTC_8x6, .isSRGB: true }; |
280 | |
281 | case QInternalGLTextureFormat::RGBA_ASTC_8x8: |
282 | return { .rhiFormat: QRhiTexture::ASTC_8x8, .isSRGB: false }; |
283 | case QInternalGLTextureFormat::SRGB8_Alpha8_ASTC_8x8: |
284 | return { .rhiFormat: QRhiTexture::ASTC_8x8, .isSRGB: true }; |
285 | |
286 | case QInternalGLTextureFormat::RGBA_ASTC_10x5: |
287 | return { .rhiFormat: QRhiTexture::ASTC_10x5, .isSRGB: false }; |
288 | case QInternalGLTextureFormat::SRGB8_Alpha8_ASTC_10x5: |
289 | return { .rhiFormat: QRhiTexture::ASTC_10x5, .isSRGB: true }; |
290 | |
291 | case QInternalGLTextureFormat::RGBA_ASTC_10x6: |
292 | return { .rhiFormat: QRhiTexture::ASTC_10x6, .isSRGB: false }; |
293 | case QInternalGLTextureFormat::SRGB8_Alpha8_ASTC_10x6: |
294 | return { .rhiFormat: QRhiTexture::ASTC_10x6, .isSRGB: true }; |
295 | |
296 | case QInternalGLTextureFormat::RGBA_ASTC_10x8: |
297 | return { .rhiFormat: QRhiTexture::ASTC_10x8, .isSRGB: false }; |
298 | case QInternalGLTextureFormat::SRGB8_Alpha8_ASTC_10x8: |
299 | return { .rhiFormat: QRhiTexture::ASTC_10x8, .isSRGB: true }; |
300 | |
301 | case QInternalGLTextureFormat::RGBA_ASTC_10x10: |
302 | return { .rhiFormat: QRhiTexture::ASTC_10x10, .isSRGB: false }; |
303 | case QInternalGLTextureFormat::SRGB8_Alpha8_ASTC_10x10: |
304 | return { .rhiFormat: QRhiTexture::ASTC_10x10, .isSRGB: true }; |
305 | |
306 | case QInternalGLTextureFormat::RGBA_ASTC_12x10: |
307 | return { .rhiFormat: QRhiTexture::ASTC_12x10, .isSRGB: false }; |
308 | case QInternalGLTextureFormat::SRGB8_Alpha8_ASTC_12x10: |
309 | return { .rhiFormat: QRhiTexture::ASTC_12x10, .isSRGB: true }; |
310 | |
311 | case QInternalGLTextureFormat::RGBA_ASTC_12x12: |
312 | return { .rhiFormat: QRhiTexture::ASTC_12x12, .isSRGB: false }; |
313 | case QInternalGLTextureFormat::SRGB8_Alpha8_ASTC_12x12: |
314 | return { .rhiFormat: QRhiTexture::ASTC_12x12, .isSRGB: true }; |
315 | |
316 | default: |
317 | return { .rhiFormat: QRhiTexture::UnknownFormat, .isSRGB: false }; |
318 | } |
319 | } |
320 | |
321 | QRhiTexture *QSGCompressedTexture::rhiTexture() const |
322 | { |
323 | return m_texture; |
324 | } |
325 | |
326 | void QSGCompressedTexture::commitTextureOperations(QRhi *rhi, QRhiResourceUpdateBatch *resourceUpdates) |
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 | |
338 | FormatInfo fmt = formatInfo(glTextureFormat: m_textureData.glInternalFormat()); |
339 | if (fmt.rhiFormat == QRhiTexture::UnknownFormat) { |
340 | qWarning(msg: "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(format: fmt.rhiFormat, flags: texFlags)) { |
350 | qCDebug(QSG_LOG_TEXTUREIO, "Compressed texture format possibly unsupported: 0x%x" , |
351 | m_textureData.glInternalFormat()); |
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(msg: "Unsupported compressed texture format 0x%x" , m_textureData.glInternalFormat()); |
356 | return; |
357 | } |
358 | } |
359 | |
360 | m_texture = rhi->newTexture(format: fmt.rhiFormat, pixelSize: m_size, sampleCount: 1, flags: texFlags); |
361 | if (!m_texture->create()) { |
362 | qWarning(msg: "Failed to create QRhiTexture for compressed data with format 0x%x" , |
363 | m_textureData.glInternalFormat()); |
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 | tex: m_texture, |
373 | desc: QRhiTextureUploadEntry(0, 0, |
374 | QRhiTextureSubresourceUploadDescription( |
375 | m_textureData.getDataView().toByteArray()))); |
376 | |
377 | m_textureData = QTextureFileData(); // Release this memory, not needed anymore |
378 | } |
379 | |
380 | QTextureFileData QSGCompressedTexture::textureData() const |
381 | { |
382 | return m_textureData; |
383 | } |
384 | |
385 | bool QSGCompressedTexture::formatIsOpaque(quint32 glTextureFormat) |
386 | { |
387 | switch (glTextureFormat) { |
388 | case QInternalGLTextureFormat::RGB_DXT1: |
389 | case QInternalGLTextureFormat::R_ATI1N_UNorm: |
390 | case QInternalGLTextureFormat::R_ATI1N_SNorm: |
391 | case QInternalGLTextureFormat::RG_ATI2N_UNorm: |
392 | case QInternalGLTextureFormat::RG_ATI2N_SNorm: |
393 | case QInternalGLTextureFormat::RGB_BP_UNSIGNED_FLOAT: |
394 | case QInternalGLTextureFormat::RGB_BP_SIGNED_FLOAT: |
395 | case QInternalGLTextureFormat::R11_EAC_UNorm: |
396 | case QInternalGLTextureFormat::R11_EAC_SNorm: |
397 | case QInternalGLTextureFormat::RG11_EAC_UNorm: |
398 | case QInternalGLTextureFormat::RG11_EAC_SNorm: |
399 | case QInternalGLTextureFormat::RGB8_ETC2: |
400 | case QInternalGLTextureFormat::SRGB8_ETC2: |
401 | case QInternalGLTextureFormat::RGB8_ETC1: |
402 | case QInternalGLTextureFormat::SRGB_DXT1: |
403 | return true; |
404 | break; |
405 | default: |
406 | return false; |
407 | } |
408 | } |
409 | |
410 | QSGCompressedTextureFactory::QSGCompressedTextureFactory(const QTextureFileData &texData) |
411 | : m_textureData(texData) |
412 | { |
413 | } |
414 | |
415 | QSGTexture *QSGCompressedTextureFactory::createTexture(QQuickWindow *window) const |
416 | { |
417 | if (!m_textureData.isValid()) |
418 | return nullptr; |
419 | |
420 | // attempt to atlas the texture |
421 | QSGRenderContext *context = QQuickWindowPrivate::get(c: window)->context; |
422 | QSGTexture *t = context->compressedTextureForFactory(this); |
423 | if (t) |
424 | return t; |
425 | |
426 | return new QSGCompressedTexture(m_textureData); |
427 | } |
428 | |
429 | int QSGCompressedTextureFactory::textureByteCount() const |
430 | { |
431 | return m_textureData.getDataView().size(); |
432 | } |
433 | |
434 | QSize QSGCompressedTextureFactory::textureSize() const |
435 | { |
436 | return m_textureData.size(); |
437 | } |
438 | |
439 | QT_END_NAMESPACE |
440 | |
441 | #include "moc_qsgcompressedtexture_p.cpp" |
442 | |