From 63e564c1780511ed2b61cad52962ef402347b3c3 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Sun, 16 Apr 2017 13:39:23 -0700 Subject: [PATCH] Add source hash to KTX metadata --- libraries/gpu/src/gpu/Texture.h | 3 +++ libraries/gpu/src/gpu/Texture_ktx.cpp | 7 +++++- libraries/image/src/image/Image.cpp | 25 ++++++------------- libraries/image/src/image/Image.h | 4 +-- .../src/model-networking/TextureCache.cpp | 9 +++---- 5 files changed, 20 insertions(+), 28 deletions(-) diff --git a/libraries/gpu/src/gpu/Texture.h b/libraries/gpu/src/gpu/Texture.h index 898b63c73c..34c4dd6f52 100755 --- a/libraries/gpu/src/gpu/Texture.h +++ b/libraries/gpu/src/gpu/Texture.h @@ -452,6 +452,8 @@ public: // For convenience assign a source name const std::string& source() const { return _source; } void setSource(const std::string& source) { _source = source; } + const std::string& sourceHash() const { return _sourceHash; } + void setSourceHash(const std::string& sourceHash) { _sourceHash = sourceHash; } // Potentially change the minimum mip (mostly for debugging purpose) bool setMinMip(uint16 newMinMip); @@ -536,6 +538,7 @@ protected: std::weak_ptr _fallback; // Not strictly necessary, but incredibly useful for debugging std::string _source; + std::string _sourceHash; std::unique_ptr< Storage > _storage; Stamp _stamp { 0 }; diff --git a/libraries/gpu/src/gpu/Texture_ktx.cpp b/libraries/gpu/src/gpu/Texture_ktx.cpp index dc8e69d302..d8dd897101 100644 --- a/libraries/gpu/src/gpu/Texture_ktx.cpp +++ b/libraries/gpu/src/gpu/Texture_ktx.cpp @@ -23,6 +23,7 @@ struct GPUKTXPayload { Texture::Usage _usage; TextureUsageType _usageType; + static std::string KEY; static bool isGPUKTX(const ktx::KeyValue& val) { return (val._key.compare(KEY) == 0); @@ -161,7 +162,11 @@ ktx::KTXUniquePointer Texture::serialize(const Texture& texture) { keyval._usage = texture.getUsage(); keyval._usageType = texture.getUsageType(); ktx::KeyValues keyValues; - keyValues.emplace_back(ktx::KeyValue(GPUKTXPayload::KEY, sizeof(GPUKTXPayload), (ktx::Byte*) &keyval)); + keyValues.emplace_back(ktx::KeyValue(GPUKTXPayload::KEY, sizeof(GPUKTXPayload), (ktx::Byte*) &keyval)); + + static const std::string SOURCE_HASH_KEY = "hifi.sourceHash"; + auto hash = texture.sourceHash(); + keyValues.emplace_back(ktx::KeyValue(SOURCE_HASH_KEY, hash.size(), (ktx::Byte*) hash.c_str())); auto ktxBuffer = ktx::KTX::create(header, images, keyValues); #if 0 diff --git a/libraries/image/src/image/Image.cpp b/libraries/image/src/image/Image.cpp index d64601d4ac..4c2d1b0857 100644 --- a/libraries/image/src/image/Image.cpp +++ b/libraries/image/src/image/Image.cpp @@ -25,8 +25,6 @@ using namespace gpu; -// FIXME: Declare this to enable compression -//#define COMPRESS_TEXTURES #define CPU_MIPMAPS 1 static const glm::uvec2 SPARSE_PAGE_SIZE(128); @@ -114,10 +112,9 @@ TextureLoader getTextureLoaderForType(gpu::TextureType type, const QVariantMap& } } -gpu::Texture* processImage(const QByteArray& content, const QUrl& url, const std::string& hash, int maxNumPixels, const TextureLoader& loader) { +gpu::Texture* processImage(const QByteArray& content, const std::string& filename, int maxNumPixels, gpu::TextureType textureType) { // Help the QImage loader by extracting the image file format from the url filename ext. // Some tga are not created properly without it. - auto filename = url.fileName().toStdString(); auto filenameExtension = filename.substr(filename.find_last_of('.') + 1); QImage image = QImage::fromData(content, filenameExtension.c_str()); int imageWidth = image.width(); @@ -126,7 +123,7 @@ gpu::Texture* processImage(const QByteArray& content, const QUrl& url, const std // Validate that the image loaded if (imageWidth == 0 || imageHeight == 0 || image.format() == QImage::Format_Invalid) { QString reason(filenameExtension.empty() ? "" : "(no file extension)"); - qCWarning(imagelogging) << "Failed to load" << url << reason; + qCWarning(imagelogging) << "Failed to load" << filename.c_str() << reason; return nullptr; } @@ -139,12 +136,15 @@ gpu::Texture* processImage(const QByteArray& content, const QUrl& url, const std imageHeight = (int)(scaleFactor * (float)imageHeight + 0.5f); QImage newImage = image.scaled(QSize(imageWidth, imageHeight), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); image.swap(newImage); - qCDebug(imagelogging).nospace() << "Downscaled " << url << " (" << + qCDebug(imagelogging).nospace() << "Downscaled " << filename.c_str() << " (" << QSize(originalWidth, originalHeight) << " to " << QSize(imageWidth, imageHeight) << ")"; } - return loader(image, url.toString().toStdString()); + auto loader = getTextureLoaderForType(textureType); + auto texture = loader(image, filename); + + return texture; } @@ -352,7 +352,6 @@ gpu::Texture* TextureUsage::process2DTextureColorFromImage(const QImage& srcImag theTexture->setUsage(usage.build()); theTexture->setStoredMipFormat(formatMip); generateMips(theTexture, image, validAlpha, alphaAsMask, false, false); - theTexture->setSource(srcImageName); } return theTexture; @@ -399,8 +398,6 @@ gpu::Texture* TextureUsage::createNormalTextureFromNormalImage(const QImage& src theTexture->setSource(srcImageName); theTexture->setStoredMipFormat(formatMip); generateMips(theTexture, image, false, false, false, true); - - theTexture->setSource(srcImageName); } return theTexture; @@ -488,8 +485,6 @@ gpu::Texture* TextureUsage::createNormalTextureFromBumpImage(const QImage& srcIm theTexture->setSource(srcImageName); theTexture->setStoredMipFormat(formatMip); generateMips(theTexture, image, false, false, false, true); - - theTexture->setSource(srcImageName); } return theTexture; @@ -519,8 +514,6 @@ gpu::Texture* TextureUsage::createRoughnessTextureFromImage(const QImage& srcIma theTexture->setSource(srcImageName); theTexture->setStoredMipFormat(formatMip); generateMips(theTexture, image, false, false, true, false); - - theTexture->setSource(srcImageName); } return theTexture; @@ -554,8 +547,6 @@ gpu::Texture* TextureUsage::createRoughnessTextureFromGlossImage(const QImage& s theTexture->setSource(srcImageName); theTexture->setStoredMipFormat(formatMip); generateMips(theTexture, image, false, false, true, false); - - theTexture->setSource(srcImageName); } return theTexture; @@ -586,8 +577,6 @@ gpu::Texture* TextureUsage::createMetallicTextureFromImage(const QImage& srcImag theTexture->setSource(srcImageName); theTexture->setStoredMipFormat(formatMip); generateMips(theTexture, image, false, false, true, false); - - theTexture->setSource(srcImageName); } return theTexture; diff --git a/libraries/image/src/image/Image.h b/libraries/image/src/image/Image.h index 391f871542..8a509e96d0 100644 --- a/libraries/image/src/image/Image.h +++ b/libraries/image/src/image/Image.h @@ -18,7 +18,6 @@ class QByteArray; class QImage; -class QUrl; namespace image { @@ -26,7 +25,7 @@ using TextureLoader = std::functiongetTextureType()); - texture.reset(image::processImage(_content, _url, hash, _maxNumPixels, loader)); - texture->setSource(_url.toString().toStdString()); - if (texture) { - texture->setFallbackTexture(networkTexture->getFallbackTexture()); - } + texture.reset(image::processImage(_content, _url.toString().toStdString(), _maxNumPixels, networkTexture->getTextureType())); + texture->setSourceHash(hash); + texture->setFallbackTexture(networkTexture->getFallbackTexture()); } // Save the image into a KTXFile