Update 'caching' of textures to KTX file backing

This commit is contained in:
Brad Davis 2017-02-22 15:59:05 -08:00
parent 81cee57bb1
commit 055db531b1

View file

@ -89,66 +89,54 @@ gpu::Texture* cacheTexture(const std::string& name, gpu::Texture* srcTexture, bo
if (!srcTexture) { if (!srcTexture) {
return nullptr; return nullptr;
} }
gpu::Texture* returnedTexture = srcTexture;
#if 0 static QString ktxCacheFolder;
auto theKTX = Texture::serialize(*srcTexture); static std::once_flag once;
if (theKTX) { std::call_once(once, [&] {
// Prepare cache directory // Prepare cache directory
QString path("hifi_ktx/"); static const QString HIFI_KTX_FOLDER("hifi_ktx");
QFileInfo originalFileInfo(path);
QString docsLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); QString docsLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
path = docsLocation + "/" + path; ktxCacheFolder = docsLocation + "/" + HIFI_KTX_FOLDER;
QFileInfo info(path); QFileInfo info(ktxCacheFolder);
if (!info.absoluteDir().exists()) { if (!info.exists()) {
QString originalRelativePath = originalFileInfo.path(); QDir(docsLocation).mkpath(HIFI_KTX_FOLDER);
QDir(docsLocation).mkpath(originalRelativePath);
} }
});
std::string cleanedName = name;
cleanedName = cleanedName.substr(cleanedName.find_last_of('//') + 1); std::string cleanedName = name;
cleanedName = cleanedName.substr(cleanedName.find_last_of('//') + 1);
std::string cacheFilename(ktxCacheFolder.toStdString());
cacheFilename += cleanedName;
cacheFilename += ".ktx";
std::string filename(path.toStdString()); gpu::Texture* returnedTexture = srcTexture;
filename += cleanedName; {
filename += ".ktx"; if (write && !QFileInfo(cacheFilename.c_str()).exists()) {
auto ktxMemory = gpu::Texture::serialize(*srcTexture);
if (write) { if (ktxMemory) {
/* FILE *file = fopen(name.c_str(), "r"); const auto& ktxStorage = ktxMemory->getStorage();
if (file != nullptr) { auto header = ktxMemory->getHeader();
fclose(file); QFile outFile(cacheFilename.c_str());
} else*/ { if (!outFile.open(QFile::Truncate | QFile::ReadWrite)) {
FILE *file = fopen (filename.c_str(),"wb"); throw std::runtime_error("Unable to open file");
if (file != nullptr) {
fwrite(theKTX->_storage->data(), 1, theKTX->_storage->size(), file);
fclose (file);
} }
//auto ktxSize = sizeof(ktx::Header); // ktxStorage->size()
auto ktxSize = ktxStorage->size();
outFile.resize(ktxSize);
auto dest = outFile.map(0, ktxSize);
memcpy(dest, ktxStorage->data(), ktxSize);
outFile.unmap(dest);
outFile.close();
} }
} }
if (read) { if (read && QFileInfo(cacheFilename.c_str()).exists()) {
FILE* file = fopen (filename.c_str(),"rb"); auto ktxFile = ktx::KTX::create(std::unique_ptr<storage::Storage>(new storage::FileStorage(cacheFilename.c_str())));
if (file != nullptr) { returnedTexture->setKtxBacking(ktxFile);
// obtain file size:
fseek (file , 0 , SEEK_END);
auto size = ftell(file);
rewind(file);
std::unique_ptr<ktx::Storage> storage(new ktx::Storage(size));
fread(storage->_bytes, 1, storage->_size, file);
fclose (file);
//then create a new texture out of the ktx
auto theNewTexure = Texture::unserialize(srcTexture->getUsage(), srcTexture->getUsageType(), ktx::KTX::create(storage), srcTexture->getSampler());
if (theNewTexure) {
returnedTexture = theNewTexure;
delete srcTexture;
}
}
} }
} }
#endif
return returnedTexture; return returnedTexture;
} }