Cleanup NetworkTexture ktx handling

This commit is contained in:
Ryan Huffman 2017-04-17 10:37:21 -07:00 committed by Atlante45
parent ab7099b3eb
commit b21dc12cc6
3 changed files with 35 additions and 17 deletions

View file

@ -75,9 +75,17 @@ std::shared_ptr<storage::FileStorage> KtxStorage::maybeOpenFile() {
return file; return file;
} }
std::lock_guard<std::mutex> lock { _cacheFileCreateMutex }; {
file = std::make_shared<storage::FileStorage>(_filename.c_str()); std::lock_guard<std::mutex> lock{ _cacheFileCreateMutex };
_cacheFile = file;
file = _cacheFile.lock();
if (file) {
return file;
}
file = std::make_shared<storage::FileStorage>(_filename.c_str());
_cacheFile = file;
}
return file; return file;
} }
@ -127,11 +135,16 @@ void KtxStorage::assignMipData(uint16 level, const storage::StoragePointer& stor
data += file->size(); data += file->size();
// TODO Cache this data inside Image or ImageDescriptor? // TODO Cache this data inside Image or ImageDescriptor?
for (auto i = _ktxDescriptor->header.numberOfMipmapLevels - 1; i >= level; --i) { for (int i = _ktxDescriptor->header.numberOfMipmapLevels - 1; i >= level; --i) {
data -= _ktxDescriptor->images[i]._imageSize; data -= _ktxDescriptor->images[i]._imageSize;
data -= 4; data -= 4;
} }
data += 4; data += 4;
data = file->mutableData();
data += ktx::KTX_HEADER_SIZE + _ktxDescriptor->header.bytesOfKeyValueData + _ktxDescriptor->images[level]._imageOffset;
data += 4;
{ {
std::lock_guard<std::mutex> lock { _cacheFileWriteMutex }; std::lock_guard<std::mutex> lock { _cacheFileWriteMutex };

View file

@ -376,11 +376,11 @@ void NetworkTexture::makeRequest() {
} }
void NetworkTexture::handleMipInterestCallback(uint16_t level) { void NetworkTexture::handleMipInterestCallback(uint16_t level) {
QMetaObject::invokeMethod(this, "handleMipInterestLevel", Qt::QueuedConnection, Q_ARG(uint16_t, level)); QMetaObject::invokeMethod(this, "handleMipInterestLevel", Qt::QueuedConnection, Q_ARG(int, level));
} }
void NetworkTexture::handleMipInterestLevel(uint16_t level) { void NetworkTexture::handleMipInterestLevel(int level) {
_lowestRequestedMipLevel = std::min(level, _lowestRequestedMipLevel); _lowestRequestedMipLevel = std::min(static_cast<uint16_t>(level), _lowestRequestedMipLevel);
if (!_ktxMipRequest) { if (!_ktxMipRequest) {
startRequestForNextMipLevel(); startRequestForNextMipLevel();
} }
@ -414,10 +414,10 @@ void NetworkTexture::startMipRangeRequest(uint16_t low, uint16_t high) {
} else { } else {
// TODO: Discover range for other mips // TODO: Discover range for other mips
ByteRange range; ByteRange range;
range.fromInclusive = ktx::KTX_HEADER_SIZE + _ktxDescriptor->header.bytesOfKeyValueData range.fromInclusive = ktx::KTX_HEADER_SIZE + _originalKtxDescriptor->header.bytesOfKeyValueData
+ _ktxDescriptor->images[low]._imageOffset + 4; + _originalKtxDescriptor->images[low]._imageOffset + 4;
range.toExclusive = ktx::KTX_HEADER_SIZE + _ktxDescriptor->header.bytesOfKeyValueData range.toExclusive = ktx::KTX_HEADER_SIZE + _originalKtxDescriptor->header.bytesOfKeyValueData
+ _ktxDescriptor->images[high + 1]._imageOffset; + _originalKtxDescriptor->images[high + 1]._imageOffset;
_ktxMipRequest->setByteRange(range); _ktxMipRequest->setByteRange(range);
} }
@ -487,6 +487,9 @@ void NetworkTexture::maybeCreateKTX() {
auto keyValues = ktx::KTX::parseKeyValues(header->bytesOfKeyValueData, reinterpret_cast<const ktx::Byte*>(_ktxHeaderData.data()) + ktx::KTX_HEADER_SIZE); auto keyValues = ktx::KTX::parseKeyValues(header->bytesOfKeyValueData, reinterpret_cast<const ktx::Byte*>(_ktxHeaderData.data()) + ktx::KTX_HEADER_SIZE);
auto imageDescriptors = header->generateImageDescriptors();
_originalKtxDescriptor.reset(new ktx::KTXDescriptor(*header, keyValues, imageDescriptors));
// Create bare ktx in memory // Create bare ktx in memory
std::string filename = "test"; std::string filename = "test";
auto memKtx = ktx::KTX::createBare(*header, keyValues); auto memKtx = ktx::KTX::createBare(*header, keyValues);
@ -507,16 +510,17 @@ void NetworkTexture::maybeCreateKTX() {
_file = file; _file = file;
} }
_ktxDescriptor.reset(new ktx::KTXDescriptor(memKtx->toDescriptor())); //_ktxDescriptor.reset(new ktx::KTXDescriptor(memKtx->toDescriptor()));
auto newKtxDescriptor = memKtx->toDescriptor();
//auto texture = gpu::Texture::serializeHeader("test.ktx", *header, keyValues); //auto texture = gpu::Texture::serializeHeader("test.ktx", *header, keyValues);
gpu::TexturePointer texture; gpu::TexturePointer texture;
texture.reset(gpu::Texture::unserialize(_file->getFilepath(), *_ktxDescriptor)); texture.reset(gpu::Texture::unserialize(_file->getFilepath(), newKtxDescriptor));
texture->setKtxBacking(file->getFilepath()); texture->setKtxBacking(file->getFilepath());
texture->setSource(filename); texture->setSource(filename);
texture->registerMipInterestListener(this); texture->registerMipInterestListener(this);
auto& images = _ktxDescriptor->images; auto& images = _originalKtxDescriptor->images;
size_t imageSizeRemaining = _ktxHighMipData.size(); size_t imageSizeRemaining = _ktxHighMipData.size();
uint8_t* ktxData = reinterpret_cast<uint8_t*>(_ktxHighMipData.data()); uint8_t* ktxData = reinterpret_cast<uint8_t*>(_ktxHighMipData.data());
ktxData += _ktxHighMipData.size(); ktxData += _ktxHighMipData.size();
@ -544,7 +548,7 @@ void NetworkTexture::maybeCreateKTX() {
} }
_lowestKnownPopulatedMip = _ktxDescriptor->header.numberOfMipmapLevels; _lowestKnownPopulatedMip = _originalKtxDescriptor->header.numberOfMipmapLevels;
for (uint16_t l = 0; l < 200; l++) { for (uint16_t l = 0; l < 200; l++) {
if (texture->isStoredMipFaceAvailable(l)) { if (texture->isStoredMipFaceAvailable(l)) {
_lowestKnownPopulatedMip = l; _lowestKnownPopulatedMip = l;
@ -554,6 +558,7 @@ void NetworkTexture::maybeCreateKTX() {
setImage(texture, header->getPixelWidth(), header->getPixelHeight()); setImage(texture, header->getPixelWidth(), header->getPixelHeight());
return;
// Force load the next two levels // Force load the next two levels
{ {

View file

@ -58,7 +58,7 @@ public:
gpu::TexturePointer getFallbackTexture() const; gpu::TexturePointer getFallbackTexture() const;
void handleMipInterestCallback(uint16_t level) override; void handleMipInterestCallback(uint16_t level) override;
Q_INVOKABLE void handleMipInterestLevel(uint16_t level); Q_INVOKABLE void handleMipInterestLevel(int level);
signals: signals:
void networkTextureCreated(const QWeakPointer<NetworkTexture>& self); void networkTextureCreated(const QWeakPointer<NetworkTexture>& self);
@ -114,7 +114,7 @@ private:
// We need this because the KTX that will be cached will likely include extra data // We need this because the KTX that will be cached will likely include extra data
// in its key/value data, and so will not match up with the original, causing // in its key/value data, and so will not match up with the original, causing
// mip offsets to change. // mip offsets to change.
ktx::KTXDescriptorPointer _ktxDescriptor; ktx::KTXDescriptorPointer _originalKtxDescriptor;
int _originalWidth { 0 }; int _originalWidth { 0 };