Skip to content
This repository has been archived by the owner on Nov 2, 2023. It is now read-only.

Commit

Permalink
Add texture memory usage in gltfEditor
Browse files Browse the repository at this point in the history
Change-Id: Ie24db0274dcdc1bd34bcf7b79ff4658b713945e0
Reviewed-on: https://kuesa-codereview.kdab.com/c/kuesa/kuesa/+/337
Reviewed-by: Paul Lemire <paul.lemire@kdab.com>
  • Loading branch information
Juan Jose Casafranca authored and lemirep committed Dec 4, 2019
1 parent dd1064e commit 92c5086
Show file tree
Hide file tree
Showing 8 changed files with 427 additions and 367 deletions.
28 changes: 13 additions & 15 deletions src/core/gltf2importer/textureparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,22 @@ class EmbeddedTextureImageFunctor : public Qt3DRender::QTextureImageDataGenerato
QImage m_image;
};

class EmbeddedTextureImage : public Qt3DRender::QAbstractTextureImage
{
public:
EmbeddedTextureImage(const QImage &image, QNode *parent = nullptr)
: Qt3DRender::QAbstractTextureImage(parent), m_image(image)
{
}
} // namespace

Qt3DRender::QTextureImageDataGeneratorPtr dataGenerator() const
{
return Qt3DRender::QTextureImageDataGeneratorPtr(new EmbeddedTextureImageFunctor(m_image));
}
TextureParser::EmbeddedTextureImage::EmbeddedTextureImage(const QImage &image, Qt3DCore::QNode *parent)
: Qt3DRender::QAbstractTextureImage(parent), m_image(image)
{
}

private:
QImage m_image;
};
Qt3DRender::QTextureImageDataGeneratorPtr TextureParser::EmbeddedTextureImage::dataGenerator() const
{
return Qt3DRender::QTextureImageDataGeneratorPtr(new EmbeddedTextureImageFunctor(m_image));
}

} // namespace
QImage TextureParser::EmbeddedTextureImage::image()
{
return m_image;
}

bool TextureParser::parse(const QJsonArray &texturesArray, GLTF2Context *context) const
{
Expand Down
14 changes: 13 additions & 1 deletion src/core/gltf2importer/textureparser_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
//

#include <QJsonArray>
#include <Qt3DRender/QAbstractTextureImage>

QT_BEGIN_NAMESPACE

namespace Qt3DRender {
class QAbstractTexture;
class QAbstractTextureImage;
} // namespace Qt3DRender

namespace Kuesa {
Expand All @@ -64,6 +64,18 @@ class Q_AUTOTEST_EXPORT TextureParser
bool parse(const QJsonArray &texturesArray, GLTF2Context *context) const;
static bool ensureImageIsCompatibleWithTexture(Qt3DRender::QAbstractTextureImage *image,
Qt3DRender::QAbstractTexture *texture);

class EmbeddedTextureImage : public Qt3DRender::QAbstractTextureImage
{
public:
EmbeddedTextureImage(const QImage &image, QNode *parent = nullptr);
Qt3DRender::QTextureImageDataGeneratorPtr dataGenerator() const;

QImage image();

private:
QImage m_image;
};
};

} // namespace GLTF2Import
Expand Down
42 changes: 42 additions & 0 deletions tools/gltfEditor/memoryusagewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
#include <Qt3DRender/QGeometryRenderer>
#include <Qt3DRender/QGeometry>

#include <Kuesa/private/textureparser_p.h>
#include "textureinspector.h"

namespace {
const QLatin1String LASTPATHSETTING("mainwindow/lastPath");

Expand Down Expand Up @@ -77,6 +80,26 @@ void MemoryUsageWidget::setSceneEntity(Kuesa::SceneEntity *sceneEntity)
}

void MemoryUsageWidget::updateWidgetValues()
{
updateGeometryMemoryUsage();

Kuesa::TextureCollection *texturesCollection = m_sceneEntity->textures();
const auto &names = texturesCollection->names();

// Textures are loaded by the backend, so we dont have yet the data for the
// texture Instead of calling directly updateTextureMemoryUsage, we wait
// until we receive a statusChanged notification
for (const auto &name : qAsConst(names)) {
Qt3DRender::QAbstractTexture *texture = texturesCollection->find(name);
QObject::connect(texture, &Qt3DRender::QAbstractTexture::statusChanged,
[this] (Qt3DRender::QAbstractTexture::Status status) {
if (status == Qt3DRender::QAbstractTexture::Status::Ready)
updateTextureMemoryUsage();
});
}
}

void MemoryUsageWidget::updateGeometryMemoryUsage() const
{
Kuesa::MeshCollection *meshesCollection = m_sceneEntity->meshes();
const auto &names = meshesCollection->names();
Expand All @@ -97,3 +120,22 @@ void MemoryUsageWidget::updateWidgetValues()

m_ui->geometryUsage->setText(totalSizeString(geometrySize));
}

void MemoryUsageWidget::updateTextureMemoryUsage() const
{
Kuesa::TextureCollection *texturesCollection = m_sceneEntity->textures();
const auto &names = texturesCollection->names();

int texturesSize = 0;
QVector<Qt3DRender::QAbstractTextureImage*> visitedImages;
for (const auto &name : qAsConst(names)) {
Qt3DRender::QAbstractTexture *texture = texturesCollection->find(name);
Qt3DRender::QAbstractTextureImage *image = texture->textureImages()[0];
if (!visitedImages.contains(image)) {
visitedImages.push_back(image);
texturesSize += ::textureSizeInBytes(texture);
}
}

m_ui->textureUsage->setText(totalSizeString(texturesSize));
}
2 changes: 2 additions & 0 deletions tools/gltfEditor/memoryusagewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class MemoryUsageWidget : public QWidget

private:
void updateWidgetValues();
void updateGeometryMemoryUsage() const;
void updateTextureMemoryUsage() const;

QScopedPointer<Ui::MemoryUsageWidget> m_ui;
Kuesa::SceneEntity *m_sceneEntity;
Expand Down
29 changes: 29 additions & 0 deletions tools/gltfEditor/textureinspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@

#include "textureinspector.h"
#include <QMetaEnum>
#include <QFileInfo>

#include <Kuesa/private/textureparser_p.h>

namespace {

Expand Down Expand Up @@ -156,6 +159,11 @@ QString TextureInspector::magnificationFilter() const
return (m_texture != nullptr) ? filtersToName.value(m_texture->magnificationFilter(), QStringLiteral("Undefined")) : QStringLiteral("Undefined");
}

int TextureInspector::textureSize() const
{
return ::textureSizeInBytes(m_texture);
}

TextureImagesModel::TextureImagesModel(QObject *parent)
: QAbstractTableModel(parent)
, m_texture(nullptr)
Expand Down Expand Up @@ -226,3 +234,24 @@ QVariant TextureImagesModel::headerData(int section, Qt::Orientation orientation
return columnsToName.value(section);
return QVariant();
}

int textureSizeInBytes(Qt3DRender::QAbstractTexture *texture)
{
auto mipmapMultiplier = 1.0;
if (texture->generateMipMaps())
// A mipmap chain uses 1/3 more space than the normal image
mipmapMultiplier = 4.0/3.0;
// texture can be of two types. QTextureLoader or QTexture2D
// We use QTextureLoader for dds files and QTexture2D for png/jpg files
auto textureLoader = qobject_cast<Qt3DRender::QTextureLoader *>(texture);
if (textureLoader) {
QFileInfo fInfo;
fInfo.setFile(textureLoader->source().toString());
if (fInfo.exists())
return static_cast<int>(mipmapMultiplier*fInfo.size());
} else {
// FIXME Check that all textures we upload coming from a glTF file have 4 channels
return static_cast<int>(mipmapMultiplier * texture->width() * texture->height() * 4);
}
return std::numeric_limits<int>::quiet_NaN();
}
3 changes: 3 additions & 0 deletions tools/gltfEditor/textureinspector.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class AbstractAssetCollection;

class TextureImagesModel;

int textureSizeInBytes(Qt3DRender::QAbstractTexture *texture);

class TextureInspector : public QObject
{
Q_OBJECT
Expand All @@ -65,6 +67,7 @@ class TextureInspector : public QObject
QString wrapZMode() const;
QString minificationFilter() const;
QString magnificationFilter() const;
int textureSize() const;

Q_SIGNALS:
void textureParamsChanged();
Expand Down
18 changes: 18 additions & 0 deletions tools/gltfEditor/texturewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@
#include <QScrollBar>
#include <QQmlContext>

namespace {
QString totalSizeString(int sizeInBytes)
{
auto size = sizeInBytes / 1024.0;
QString suffix(QStringLiteral(" KB"));
if (size > 1024.) {
size = size / 1024.;
suffix = QStringLiteral(" MB");
}
return QString::number(size, 'f', 2) + suffix;
}
}

TextureWidget::TextureWidget(QWidget *parent)
: QWidget(parent)
, m_inspector(nullptr)
Expand All @@ -48,6 +61,10 @@ TextureWidget::TextureWidget(QWidget *parent)
{
ui->setupUi(this);

auto scrollArea = new FixedWidthScrollArea(this);
layout()->addWidget(scrollArea);
scrollArea->setWidget(ui->rootWidget);

m_previewWidget->setAlignment(Qt::AlignHCenter);
auto hBoxLayout = new QHBoxLayout(ui->texturePreviewGroupBox);
hBoxLayout->addWidget(m_previewWidget);
Expand Down Expand Up @@ -81,6 +98,7 @@ void TextureWidget::updateData()
{
ui->nameValue->setText(m_inspector->assetName());
ui->formatValue->setText(m_inspector->format());
ui->textureSize->setText(::totalSizeString(m_inspector->textureSize()));
ui->targetValue->setText(m_inspector->target());
ui->widthValue->setText(QString::number(m_inspector->width()));
ui->heightValue->setText(QString::number(m_inspector->height()));
Expand Down
Loading

0 comments on commit 92c5086

Please sign in to comment.