| 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" |
| 5 | #include "qtexturefiledata_p.h" |
| 6 | |
| 7 | #include <private/qnumeric_p.h> |
| 8 | |
| 9 | #include <QFile> |
| 10 | #include <QDebug> |
| 11 | #include <QSize> |
| 12 | |
| 13 | QT_BEGIN_NAMESPACE |
| 14 | |
| 15 | struct |
| 16 | { |
| 17 | quint8 [4]; |
| 18 | quint8 ; |
| 19 | quint8 ; |
| 20 | quint8 ; |
| 21 | quint8 [3]; |
| 22 | quint8 [3]; |
| 23 | quint8 [3]; |
| 24 | }; |
| 25 | |
| 26 | QAstcHandler::~QAstcHandler() = default; |
| 27 | |
| 28 | bool QAstcHandler::canRead(const QByteArray &suffix, const QByteArray &block) |
| 29 | { |
| 30 | Q_UNUSED(suffix); |
| 31 | |
| 32 | return block.startsWith(bv: "\x13\xAB\xA1\x5C" ); |
| 33 | } |
| 34 | |
| 35 | quint32 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(varName: "QT_ASTCHANDLER_USE_SRGB" ) |
| 69 | || logName().toLower().contains(bv: "srgb" ); |
| 70 | |
| 71 | return useSrgb ? (glFormatSRGBBase + index) : (glFormatRGBABase + index); |
| 72 | } |
| 73 | |
| 74 | QTextureFileData QAstcHandler::read() |
| 75 | { |
| 76 | QTextureFileData nullData; |
| 77 | QTextureFileData res; |
| 78 | |
| 79 | if (!device()) |
| 80 | return nullData; |
| 81 | |
| 82 | QByteArray fileData = device()->readAll(); |
| 83 | if (fileData.size() < int(sizeof(AstcHeader)) || !canRead(suffix: QByteArray(), block: fileData)) { |
| 84 | qCDebug(lcQtGuiTextureIO, "Not an ASTC file: %s" , logName().constData()); |
| 85 | return nullData; |
| 86 | } |
| 87 | res.setData(fileData); |
| 88 | |
| 89 | const AstcHeader * = 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(xBlockDim: header->blockDimX, yBlockDim: 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(v1: xBlocks, v2: yBlocks, r: &byteCount) |
| 113 | || qMulOverflow(v1: byteCount, v2: zBlocks, r: &byteCount) |
| 114 | || qMulOverflow(v1: byteCount, v2: 16, r: &byteCount); |
| 115 | |
| 116 | |
| 117 | res.setDataOffset(offset: sizeof(AstcHeader)); |
| 118 | res.setNumLevels(1); |
| 119 | res.setNumFaces(1); |
| 120 | res.setDataLength(length: 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 | |
| 135 | QT_END_NAMESPACE |
| 136 | |