Better avoid overwriting textures in ModelBaker::compressTexture

This commit is contained in:
sabrina-shanman 2019-03-15 16:46:37 -07:00
parent b6c44ea443
commit 0990b56952
3 changed files with 28 additions and 29 deletions

View file

@ -410,43 +410,40 @@ QString ModelBaker::compressTexture(QString modelTextureFileName, image::Texture
} }
auto urlToTexture = getTextureURL(modelTextureFileInfo, !textureContent.isNull()); auto urlToTexture = getTextureURL(modelTextureFileInfo, !textureContent.isNull());
QString baseTextureFileName; TextureKey textureKey { urlToTexture, textureType };
if (_remappedTexturePaths.contains(urlToTexture)) { auto bakingTextureIt = _bakingTextures.find(textureKey);
baseTextureFileName = _remappedTexturePaths[urlToTexture]; if (bakingTextureIt == _bakingTextures.cend()) {
} else {
// construct the new baked texture file name and file path // construct the new baked texture file name and file path
// ensuring that the baked texture will have a unique name // ensuring that the baked texture will have a unique name
// even if there was another texture with the same name at a different path // even if there was another texture with the same name at a different path
baseTextureFileName = _textureFileNamer.createBaseTextureFileName(modelTextureFileInfo, textureType); QString baseTextureFileName = _textureFileNamer.createBaseTextureFileName(modelTextureFileInfo, textureType);
_remappedTexturePaths[urlToTexture] = baseTextureFileName;
}
qCDebug(model_baking).noquote() << "Re-mapping" << modelTextureFileName QString bakedTextureFilePath {
<< "to" << baseTextureFileName; _bakedOutputDir + "/" + baseTextureFileName + BAKED_META_TEXTURE_SUFFIX
};
QString bakedTextureFilePath { textureChild = baseTextureFileName + BAKED_META_TEXTURE_SUFFIX;
_bakedOutputDir + "/" + baseTextureFileName + BAKED_META_TEXTURE_SUFFIX
};
textureChild = baseTextureFileName + BAKED_META_TEXTURE_SUFFIX;
if (!_bakingTextures.contains(urlToTexture)) {
_outputFiles.push_back(bakedTextureFilePath); _outputFiles.push_back(bakedTextureFilePath);
// bake this texture asynchronously // bake this texture asynchronously
bakeTexture(urlToTexture, textureType, _bakedOutputDir, baseTextureFileName, textureContent); bakeTexture(textureKey, _bakedOutputDir, baseTextureFileName, textureContent);
} else {
// Fetch existing texture meta name
textureChild = (*bakingTextureIt)->getBaseFilename() + BAKED_META_TEXTURE_SUFFIX;
} }
} }
qCDebug(model_baking).noquote() << "Re-mapping" << modelTextureFileName
<< "to" << textureChild;
return textureChild; return textureChild;
} }
void ModelBaker::bakeTexture(const QUrl& textureURL, image::TextureUsage::Type textureType, void ModelBaker::bakeTexture(const TextureKey& textureKey, const QDir& outputDir, const QString& bakedFilename, const QByteArray& textureContent) {
const QDir& outputDir, const QString& bakedFilename, const QByteArray& textureContent) {
// start a bake for this texture and add it to our list to keep track of // start a bake for this texture and add it to our list to keep track of
QSharedPointer<TextureBaker> bakingTexture{ QSharedPointer<TextureBaker> bakingTexture{
new TextureBaker(textureURL, textureType, outputDir, "../", bakedFilename, textureContent), new TextureBaker(textureKey.first, textureKey.second, outputDir, "../", bakedFilename, textureContent),
&TextureBaker::deleteLater &TextureBaker::deleteLater
}; };
@ -455,7 +452,7 @@ void ModelBaker::bakeTexture(const QUrl& textureURL, image::TextureUsage::Type t
connect(bakingTexture.data(), &TextureBaker::aborted, this, &ModelBaker::handleAbortedTexture); connect(bakingTexture.data(), &TextureBaker::aborted, this, &ModelBaker::handleAbortedTexture);
// keep a shared pointer to the baking texture // keep a shared pointer to the baking texture
_bakingTextures.insert(textureURL, bakingTexture); _bakingTextures.insert(textureKey, bakingTexture);
// start baking the texture on one of our available worker threads // start baking the texture on one of our available worker threads
bakingTexture->moveToThread(_textureThreadGetter()); bakingTexture->moveToThread(_textureThreadGetter());
@ -507,7 +504,7 @@ void ModelBaker::handleBakedTexture() {
// now that this texture has been baked and handled, we can remove that TextureBaker from our hash // now that this texture has been baked and handled, we can remove that TextureBaker from our hash
_bakingTextures.remove(bakedTexture->getTextureURL()); _bakingTextures.remove({ bakedTexture->getTextureURL(), bakedTexture->getTextureType() });
checkIfTexturesFinished(); checkIfTexturesFinished();
} else { } else {
@ -518,7 +515,7 @@ void ModelBaker::handleBakedTexture() {
_pendingErrorEmission = true; _pendingErrorEmission = true;
// now that this texture has been baked, even though it failed, we can remove that TextureBaker from our list // now that this texture has been baked, even though it failed, we can remove that TextureBaker from our list
_bakingTextures.remove(bakedTexture->getTextureURL()); _bakingTextures.remove({ bakedTexture->getTextureURL(), bakedTexture->getTextureType() });
// abort any other ongoing texture bakes since we know we'll end up failing // abort any other ongoing texture bakes since we know we'll end up failing
for (auto& bakingTexture : _bakingTextures) { for (auto& bakingTexture : _bakingTextures) {
@ -531,7 +528,7 @@ void ModelBaker::handleBakedTexture() {
// we have errors to attend to, so we don't do extra processing for this texture // we have errors to attend to, so we don't do extra processing for this texture
// but we do need to remove that TextureBaker from our list // but we do need to remove that TextureBaker from our list
// and then check if we're done with all textures // and then check if we're done with all textures
_bakingTextures.remove(bakedTexture->getTextureURL()); _bakingTextures.remove({ bakedTexture->getTextureURL(), bakedTexture->getTextureType() });
checkIfTexturesFinished(); checkIfTexturesFinished();
} }
@ -545,7 +542,7 @@ void ModelBaker::handleAbortedTexture() {
qDebug() << "Texture aborted: " << bakedTexture->getTextureURL(); qDebug() << "Texture aborted: " << bakedTexture->getTextureURL();
if (bakedTexture) { if (bakedTexture) {
_bakingTextures.remove(bakedTexture->getTextureURL()); _bakingTextures.remove({ bakedTexture->getTextureURL(), bakedTexture->getTextureType() });
} }
// since a texture we were baking aborted, our status is also aborted // since a texture we were baking aborted, our status is also aborted

View file

@ -42,6 +42,8 @@ class ModelBaker : public Baker {
Q_OBJECT Q_OBJECT
public: public:
using TextureKey = QPair<QUrl, image::TextureUsage::Type>;
ModelBaker(const QUrl& inputModelURL, TextureBakerThreadGetter inputTextureThreadGetter, ModelBaker(const QUrl& inputModelURL, TextureBakerThreadGetter inputTextureThreadGetter,
const QString& bakedOutputDirectory, const QString& originalOutputDirectory = "", bool hasBeenBaked = false); const QString& bakedOutputDirectory, const QString& originalOutputDirectory = "", bool hasBeenBaked = false);
virtual ~ModelBaker(); virtual ~ModelBaker();
@ -99,13 +101,11 @@ private slots:
private: private:
QUrl getTextureURL(const QFileInfo& textureFileInfo, bool isEmbedded = false); QUrl getTextureURL(const QFileInfo& textureFileInfo, bool isEmbedded = false);
void bakeTexture(const QUrl & textureURL, image::TextureUsage::Type textureType, const QDir & outputDir, void bakeTexture(const TextureKey& textureKey, const QDir& outputDir, const QString& bakedFilename, const QByteArray& textureContent);
const QString & bakedFilename, const QByteArray & textureContent);
QString texturePathRelativeToModel(QUrl modelURL, QUrl textureURL); QString texturePathRelativeToModel(QUrl modelURL, QUrl textureURL);
QMultiHash<QUrl, QSharedPointer<TextureBaker>> _bakingTextures; QMultiHash<TextureKey, QSharedPointer<TextureBaker>> _bakingTextures;
QHash<QString, int> _textureNameMatchCount; QHash<QString, int> _textureNameMatchCount;
QHash<QUrl, QString> _remappedTexturePaths;
bool _pendingErrorEmission { false }; bool _pendingErrorEmission { false };
bool _hasBeenBaked { false }; bool _hasBeenBaked { false };

View file

@ -39,6 +39,8 @@ public:
QUrl getTextureURL() const { return _textureURL; } QUrl getTextureURL() const { return _textureURL; }
QString getBaseFilename() const { return _baseFilename; }
QString getMetaTextureFileName() const { return _metaTextureFileName; } QString getMetaTextureFileName() const { return _metaTextureFileName; }
virtual void setWasAborted(bool wasAborted) override; virtual void setWasAborted(bool wasAborted) override;