Add source hash to KTX metadata

This commit is contained in:
Atlante45 2017-04-16 13:39:23 -07:00
parent d9a7615cc8
commit 63e564c178
5 changed files with 20 additions and 28 deletions

View file

@ -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<Texture> _fallback;
// Not strictly necessary, but incredibly useful for debugging
std::string _source;
std::string _sourceHash;
std::unique_ptr< Storage > _storage;
Stamp _stamp { 0 };

View file

@ -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

View file

@ -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;

View file

@ -18,7 +18,6 @@
class QByteArray;
class QImage;
class QUrl;
namespace image {
@ -26,7 +25,7 @@ using TextureLoader = std::function<gpu::Texture*(const QImage&, const std::stri
TextureLoader getTextureLoaderForType(gpu::TextureType type, const QVariantMap& options = 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& url, int maxNumPixels, gpu::TextureType textureType);
namespace TextureUsage {
@ -44,7 +43,6 @@ gpu::Texture* createCubeTextureFromImageWithoutIrradiance(const QImage& image, c
gpu::Texture* createLightmapTextureFromImage(const QImage& image, const std::string& srcImageName);
const QImage process2DImageColor(const QImage& srcImage, bool& validAlpha, bool& alphaAsMask);
void defineColorTexelFormats(gpu::Element& formatGPU, gpu::Element& formatMip, const QImage& srcImage, bool isLinear);
gpu::Texture* process2DTextureColorFromImage(const QImage& srcImage, const std::string& srcImageName, bool isLinear, bool isStrict = false);
gpu::Texture* processCubeTextureColorFromImage(const QImage& srcImage, const std::string& srcImageName, bool isLinear, bool generateIrradiance);

View file

@ -432,12 +432,9 @@ void ImageReader::read() {
gpu::TexturePointer texture;
{
PROFILE_RANGE_EX(resource_parse_image_raw, __FUNCTION__, 0xffff0000, 0);
auto loader = image::getTextureLoaderForType(networkTexture->getTextureType());
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