From 47cf44dc60819842bd86816d989891490cf0e4a7 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Wed, 26 Apr 2017 17:11:19 -0700 Subject: [PATCH] Store offset to min mip kv in KtxStorage --- libraries/gpu/src/gpu/Texture.h | 1 + libraries/gpu/src/gpu/Texture_ktx.cpp | 17 ++++++++--------- libraries/ktx/src/ktx/KTX.cpp | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/libraries/gpu/src/gpu/Texture.h b/libraries/gpu/src/gpu/Texture.h index 825e9237f5..502ad33143 100755 --- a/libraries/gpu/src/gpu/Texture.h +++ b/libraries/gpu/src/gpu/Texture.h @@ -330,6 +330,7 @@ public: std::string _filename; std::atomic _minMipLevelAvailable; + size_t _offsetToMinMipKV; ktx::KTXDescriptorPointer _ktxDescriptor; friend class Texture; diff --git a/libraries/gpu/src/gpu/Texture_ktx.cpp b/libraries/gpu/src/gpu/Texture_ktx.cpp index e3c4b325fb..8181bb21ae 100644 --- a/libraries/gpu/src/gpu/Texture_ktx.cpp +++ b/libraries/gpu/src/gpu/Texture_ktx.cpp @@ -109,18 +109,18 @@ KtxStorage::KtxStorage(const std::string& filename) : _filename(filename) { if (_ktxDescriptor->images.size() < _ktxDescriptor->header.numberOfMipmapLevels) { qWarning() << "Bad images found in ktx"; } - auto& keyValues = _ktxDescriptor->keyValues; - auto found = std::find_if(keyValues.begin(), keyValues.end(), [](const ktx::KeyValue& val) -> bool { - return val._key.compare(ktx::HIFI_MIN_POPULATED_MIP_KEY) == 0; - }); - if (found != keyValues.end()) { - _minMipLevelAvailable = found->_value[0]; + + _offsetToMinMipKV = _ktxDescriptor->getValueOffsetForKey(ktx::HIFI_MIN_POPULATED_MIP_KEY); + if (_offsetToMinMipKV) { + auto data = storage->data() + ktx::KTX_HEADER_SIZE + _offsetToMinMipKV; + _minMipLevelAvailable = *data; } else { // Assume all mip levels are available _minMipLevelAvailable = 0; } } + // now that we know the ktx, let's get the header info to configure this Texture::Storage: Format mipFormat = Format::COLOR_BGRA_32; Format texelFormat = Format::COLOR_SRGBA_32; @@ -195,7 +195,6 @@ void KtxStorage::assignMipData(uint16 level, const storage::StoragePointer& stor imageData += ktx::KTX_HEADER_SIZE + _ktxDescriptor->header.bytesOfKeyValueData + _ktxDescriptor->images[level]._imageOffset; imageData += ktx::IMAGE_SIZE_WIDTH; - auto offset = _ktxDescriptor->getValueOffsetForKey(ktx::HIFI_MIN_POPULATED_MIP_KEY); { std::lock_guard lock { _cacheFileWriteMutex }; @@ -206,8 +205,8 @@ void KtxStorage::assignMipData(uint16 level, const storage::StoragePointer& stor memcpy(imageData, storage->data(), _ktxDescriptor->images[level]._imageSize); _minMipLevelAvailable = level; - if (offset > 0) { - auto minMipKeyData = file->mutableData() + ktx::KTX_HEADER_SIZE + ktx::KV_SIZE_WIDTH + offset; + if (_offsetToMinMipKV > 0) { + auto minMipKeyData = file->mutableData() + ktx::KTX_HEADER_SIZE + _offsetToMinMipKV; memcpy(minMipKeyData, (void*)&_minMipLevelAvailable, 1); } } diff --git a/libraries/ktx/src/ktx/KTX.cpp b/libraries/ktx/src/ktx/KTX.cpp index 0dbc2e720f..cea397927a 100644 --- a/libraries/ktx/src/ktx/KTX.cpp +++ b/libraries/ktx/src/ktx/KTX.cpp @@ -126,7 +126,7 @@ size_t KTXDescriptor::getValueOffsetForKey(const std::string& key) const { size_t offset { 0 }; for (auto& kv : keyValues) { if (kv._key == key) { - return offset + kv._key.size() + 1; + return offset + ktx::KV_SIZE_WIDTH + kv._key.size() + 1; } offset += kv.serializedByteSize(); }