| 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 | |
| 4 | #include "qsgcompressedatlastexture_p.h" |
| 5 | |
| 6 | #include <QtCore/QVarLengthArray> |
| 7 | #include <QtCore/QElapsedTimer> |
| 8 | #include <QtCore/QtMath> |
| 9 | |
| 10 | #include <QtGui/QGuiApplication> |
| 11 | #include <QtGui/QScreen> |
| 12 | #include <QtGui/QSurface> |
| 13 | #include <QtGui/QWindow> |
| 14 | #include <QDebug> |
| 15 | |
| 16 | #include <private/qqmlglobal_p.h> |
| 17 | #include <private/qquickprofiler_p.h> |
| 18 | #include <private/qsgtexture_p.h> |
| 19 | #include <private/qsgcompressedtexture_p.h> |
| 20 | |
| 21 | QT_BEGIN_NAMESPACE |
| 22 | |
| 23 | namespace QSGCompressedAtlasTexture |
| 24 | { |
| 25 | |
| 26 | Atlas::Atlas(QSGDefaultRenderContext *rc, const QSize &size, uint format) |
| 27 | : QSGRhiAtlasTexture::AtlasBase(rc, size), m_format(format) |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | Atlas::~Atlas() |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | Texture *Atlas::create(QByteArrayView data, const QSize &size) |
| 36 | { |
| 37 | // Align reservation to 16x16, >= any compressed block size |
| 38 | QSize paddedSize(((size.width() + 15) / 16) * 16, ((size.height() + 15) / 16) * 16); |
| 39 | // No need to lock, as manager already locked it. |
| 40 | QRect rect = m_allocator.allocate(size: paddedSize); |
| 41 | if (rect.width() > 0 && rect.height() > 0) { |
| 42 | Texture *t = new Texture(this, rect, data, size); |
| 43 | m_pending_uploads << t; |
| 44 | return t; |
| 45 | } |
| 46 | return nullptr; |
| 47 | } |
| 48 | |
| 49 | bool Atlas::generateTexture() |
| 50 | { |
| 51 | QSGCompressedTexture::FormatInfo fmt = QSGCompressedTexture::formatInfo(glTextureFormat: m_format); |
| 52 | QRhiTexture::Flags flags(QRhiTexture::UsedAsTransferSource | QRhiTexture::UsedAsCompressedAtlas); |
| 53 | flags.setFlag(flag: QRhiTexture::sRGB, on: fmt.isSRGB); |
| 54 | m_texture = m_rhi->newTexture(format: fmt.rhiFormat, pixelSize: m_size, sampleCount: 1, flags); |
| 55 | if (!m_texture) |
| 56 | return false; |
| 57 | |
| 58 | if (!m_texture->create()) { |
| 59 | delete m_texture; |
| 60 | m_texture = nullptr; |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | qCDebug(QSG_LOG_TEXTUREIO, "Created compressed atlas of size %dx%d for format 0x%x (rhi: %d)" , |
| 65 | m_size.width(), m_size.height(), m_format, fmt.rhiFormat); |
| 66 | |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | void Atlas::enqueueTextureUpload(QSGRhiAtlasTexture::TextureBase *t, QRhiResourceUpdateBatch *rcub) |
| 71 | { |
| 72 | Texture *texture = static_cast<Texture *>(t); |
| 73 | |
| 74 | const QRect &r = texture->atlasSubRect(); |
| 75 | |
| 76 | QRhiTextureSubresourceUploadDescription subresDesc(texture->data().constData(), |
| 77 | texture->sizeInBytes()); |
| 78 | subresDesc.setSourceSize(texture->textureSize()); |
| 79 | subresDesc.setDestinationTopLeft(r.topLeft()); |
| 80 | |
| 81 | QRhiTextureUploadDescription desc(QRhiTextureUploadEntry(0, 0, subresDesc)); |
| 82 | rcub->uploadTexture(tex: m_texture, desc); |
| 83 | |
| 84 | qCDebug(QSG_LOG_TEXTUREIO, "compressed atlastexture upload, size %dx%d format 0x%x" , |
| 85 | t->textureSize().width(), t->textureSize().height(), m_format); |
| 86 | } |
| 87 | |
| 88 | Texture::Texture(Atlas *atlas, const QRect &textureRect, QByteArrayView data, const QSize &size) |
| 89 | : QSGRhiAtlasTexture::TextureBase(atlas, textureRect), |
| 90 | m_nonatlas_texture(nullptr), |
| 91 | m_data(data.toByteArray()), |
| 92 | m_size(size) |
| 93 | { |
| 94 | float w = atlas->size().width(); |
| 95 | float h = atlas->size().height(); |
| 96 | const QRect &r = atlasSubRect(); |
| 97 | // offset by half-pixel to prevent bleeding when scaling |
| 98 | m_texture_coords_rect = QRectF((r.x() + .5) / w, |
| 99 | (r.y() + .5) / h, |
| 100 | (size.width() - 1.) / w, |
| 101 | (size.height() - 1.) / h); |
| 102 | } |
| 103 | |
| 104 | Texture::~Texture() |
| 105 | { |
| 106 | delete m_nonatlas_texture; |
| 107 | } |
| 108 | |
| 109 | bool Texture::hasAlphaChannel() const |
| 110 | { |
| 111 | return !QSGCompressedTexture::formatIsOpaque(glTextureFormat: static_cast<Atlas*>(m_atlas)->format()); |
| 112 | } |
| 113 | |
| 114 | QSGTexture *Texture::removedFromAtlas(QRhiResourceUpdateBatch *) const |
| 115 | { |
| 116 | if (m_nonatlas_texture) { |
| 117 | m_nonatlas_texture->setMipmapFiltering(mipmapFiltering()); |
| 118 | m_nonatlas_texture->setFiltering(filtering()); |
| 119 | return m_nonatlas_texture; |
| 120 | } |
| 121 | |
| 122 | if (!m_data.isEmpty()) { |
| 123 | QTextureFileData texData; |
| 124 | texData.setData(m_data); |
| 125 | texData.setSize(m_size); |
| 126 | texData.setGLInternalFormat(static_cast<Atlas*>(m_atlas)->format()); |
| 127 | texData.setDataLength(length: m_data.size()); |
| 128 | texData.setDataOffset(offset: 0); |
| 129 | m_nonatlas_texture = new QSGCompressedTexture(texData); |
| 130 | m_nonatlas_texture->setMipmapFiltering(mipmapFiltering()); |
| 131 | m_nonatlas_texture->setFiltering(filtering()); |
| 132 | } |
| 133 | |
| 134 | return m_nonatlas_texture; |
| 135 | } |
| 136 | |
| 137 | } |
| 138 | |
| 139 | QT_END_NAMESPACE |
| 140 | |
| 141 | #include "moc_qsgcompressedatlastexture_p.cpp" |
| 142 | |