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
qastchandler.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 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#include "qastchandler_p.h"
6
7#include <private/qnumeric_p.h>
8
9#include <QFile>
10#include <QDebug>
11#include <QSize>
12
14
25
27
28bool QAstcHandler::canRead(const QByteArray &suffix, const QByteArray &block)
29{
30 Q_UNUSED(suffix);
31
32 return block.startsWith("\x13\xAB\xA1\x5C");
33}
34
35quint32 QAstcHandler::astcGLFormat(quint8 xBlockDim, quint8 yBlockDim) const
36{
37 static const quint32 glFormatRGBABase = 0x93B0; // GL_COMPRESSED_RGBA_ASTC_4x4_KHR
38 static const quint32 glFormatSRGBBase = 0x93D0; // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR
39
40 Q_CONSTINIT static QSize dims[14] = {
41 { 4, 4 }, // GL_COMPRESSED_xxx_ASTC_4x4_KHR
42 { 5, 4 }, // GL_COMPRESSED_xxx_ASTC_5x4_KHR
43 { 5, 5 }, // GL_COMPRESSED_xxx_ASTC_5x5_KHR
44 { 6, 5 }, // GL_COMPRESSED_xxx_ASTC_6x5_KHR
45 { 6, 6 }, // GL_COMPRESSED_xxx_ASTC_6x6_KHR
46 { 8, 5 }, // GL_COMPRESSED_xxx_ASTC_8x5_KHR
47 { 8, 6 }, // GL_COMPRESSED_xxx_ASTC_8x6_KHR
48 { 8, 8 }, // GL_COMPRESSED_xxx_ASTC_8x8_KHR
49 { 10, 5 }, // GL_COMPRESSED_xxx_ASTC_10x5_KHR
50 { 10, 6 }, // GL_COMPRESSED_xxx_ASTC_10x6_KHR
51 { 10, 8 }, // GL_COMPRESSED_xxx_ASTC_10x8_KHR
52 { 10, 10 }, // GL_COMPRESSED_xxx_ASTC_10x10_KHR
53 { 12, 10 }, // GL_COMPRESSED_xxx_ASTC_12x10_KHR
54 { 12, 12 } // GL_COMPRESSED_xxx_ASTC_12x12_KHR
55 };
56
57 const QSize dim(xBlockDim, yBlockDim);
58 int index = -1;
59 for (int i = 0; i < 14; i++) {
60 if (dim == dims[i]) {
61 index = i;
62 break;
63 }
64 }
65 if (index < 0)
66 return 0;
67
68 bool useSrgb = qEnvironmentVariableIsSet("QT_ASTCHANDLER_USE_SRGB")
69 || logName().toLower().contains("srgb");
70
71 return useSrgb ? (glFormatSRGBBase + index) : (glFormatRGBABase + index);
72}
73
75{
76 QTextureFileData nullData;
78
79 if (!device())
80 return nullData;
81
82 QByteArray fileData = device()->readAll();
83 if (fileData.size() < int(sizeof(AstcHeader)) || !canRead(QByteArray(), fileData)) {
84 qCDebug(lcQtGuiTextureIO, "Not an ASTC file: %s", logName().constData());
85 return nullData;
86 }
87 res.setData(fileData);
88
89 const AstcHeader *header = reinterpret_cast<const AstcHeader *>(fileData.constData());
90
91 int xSz = int(header->xSize[0]) | int(header->xSize[1]) << 8 | int(header->xSize[2]) << 16;
92 int ySz = int(header->ySize[0]) | int(header->ySize[1]) << 8 | int(header->ySize[2]) << 16;
93 int zSz = int(header->zSize[0]) | int(header->zSize[1]) << 8 | int(header->zSize[2]) << 16;
94
95 quint32 glFmt = astcGLFormat(header->blockDimX, header->blockDimY);
96
97 if (!xSz || !ySz || !zSz || !glFmt || header->blockDimZ != 1) {
98 qCDebug(lcQtGuiTextureIO, "Invalid ASTC header data in file %s", logName().constData());
99 return nullData;
100 }
101
102 res.setSize(QSize(xSz, ySz));
103 res.setGLFormat(0); // 0 for compressed textures
104 res.setGLInternalFormat(glFmt);
105 //? BaseInternalFormat
106
107 int xBlocks = (xSz + header->blockDimX - 1) / header->blockDimX;
108 int yBlocks = (ySz + header->blockDimY - 1) / header->blockDimY;
109 int zBlocks = (zSz + header->blockDimZ - 1) / header->blockDimZ;
110
111 int byteCount = 0;
112 bool oob = qMulOverflow(xBlocks, yBlocks, &byteCount)
113 || qMulOverflow(byteCount, zBlocks, &byteCount)
114 || qMulOverflow(byteCount, 16, &byteCount);
115
116
117 res.setDataOffset(sizeof(AstcHeader));
118 res.setNumLevels(1);
119 res.setNumFaces(1);
120 res.setDataLength(byteCount);
121
122 if (oob || !res.isValid()) {
123 qCDebug(lcQtGuiTextureIO, "Invalid ASTC file %s", logName().constData());
124 return nullData;
125 }
126
127 res.setLogName(logName());
128
129#if 0
130 qDebug() << "ASTC file handler read" << res << res.dataOffset() << res.dataLength();
131#endif
132 return res;
133}
134
static bool canRead(const QByteArray &suffix, const QByteArray &block)
QTextureFileData read() override
~QAstcHandler() override
\inmodule QtCore
Definition qbytearray.h:57
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
Definition qbytearray.h:494
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:124
bool startsWith(QByteArrayView bv) const
Definition qbytearray.h:223
bool contains(char c) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qbytearray.h:660
QByteArray toLower() const &
Definition qbytearray.h:254
QByteArray readAll()
Reads all remaining data from the device, and returns it as a byte array.
\inmodule QtCore
Definition qsize.h:25
QIODevice * device() const
QByteArray logName() const
Combined button and popup list for selecting options.
static QString header(const QString &name)
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
#define qDebug
[1]
Definition qlogging.h:164
#define qCDebug(category,...)
std::enable_if_t< std::is_unsigned_v< T >||std::is_signed_v< T >, bool > qMulOverflow(T v1, T v2, T *r)
Definition qnumeric.h:182
GLuint index
[2]
GLuint res
Q_CORE_EXPORT bool qEnvironmentVariableIsSet(const char *varName) noexcept
#define Q_UNUSED(x)
unsigned int quint32
Definition qtypes.h:50
unsigned char quint8
Definition qtypes.h:46
quint8 blockDimZ
quint8 magic[4]
quint8 xSize[3]
quint8 zSize[3]
quint8 blockDimX
quint8 ySize[3]
quint8 blockDimY