From d5f1e6fb373e7c004353fd0d771da8996f3aeb08 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Wed, 19 Apr 2017 17:01:36 -0700 Subject: [PATCH] Adjust handling of requested mips in gl backend --- libraries/gpu-gl/src/gpu/gl/GLTexture.cpp | 2 +- .../gpu/gl45/GL45BackendVariableTexture.cpp | 8 ++--- libraries/gpu/src/gpu/Texture_ktx.cpp | 3 +- libraries/ktx/src/ktx/Reader.cpp | 2 +- .../src/model-networking/TextureCache.cpp | 32 +++++++++++++------ 5 files changed, 28 insertions(+), 19 deletions(-) diff --git a/libraries/gpu-gl/src/gpu/gl/GLTexture.cpp b/libraries/gpu-gl/src/gpu/gl/GLTexture.cpp index 652dc3c46f..1179403bff 100644 --- a/libraries/gpu-gl/src/gpu/gl/GLTexture.cpp +++ b/libraries/gpu-gl/src/gpu/gl/GLTexture.cpp @@ -450,7 +450,7 @@ void GLVariableAllocationSupport::updateMemoryPressure() { // Track how much we're actually using totalVariableMemoryAllocation += gltexture->size(); canDemote |= vartexture->canDemote(); - canPromote |= vartexture->canPromote(); + canPromote |= vartexture->canPromote() || (texture->minAvailableMipLevel() < vartexture->_allocatedMip); hasTransfers |= vartexture->hasPendingTransfers(); } diff --git a/libraries/gpu-gl/src/gpu/gl45/GL45BackendVariableTexture.cpp b/libraries/gpu-gl/src/gpu/gl45/GL45BackendVariableTexture.cpp index ac2b5f607f..f99209a7c2 100644 --- a/libraries/gpu-gl/src/gpu/gl45/GL45BackendVariableTexture.cpp +++ b/libraries/gpu-gl/src/gpu/gl45/GL45BackendVariableTexture.cpp @@ -98,18 +98,14 @@ void GL45ResourceTexture::copyMipsFromTexture() { void GL45ResourceTexture::syncSampler() const { Parent::syncSampler(); qDebug() << "glTextureParameteri " << QString::fromStdString(_source) << _populatedMip << _populatedMip - _allocatedMip; - if (_source == "test" && _populatedMip == 0) { - qDebug() << "here"; - } glTextureParameteri(_id, GL_TEXTURE_BASE_LEVEL, _populatedMip - _allocatedMip); } void GL45ResourceTexture::promote() { PROFILE_RANGE(render_gpu_gl, __FUNCTION__); //Q_ASSERT(_allocatedMip > 0); - uint16_t sourceMip = _populatedMip; - if (!_gpuObject.isStoredMipFaceAvailable(sourceMip, 0)) { - const_cast(_gpuObject).requestInterestInMip(sourceMip); + if (!_gpuObject.isStoredMipFaceAvailable(_populatedMip - 1, 0)) { + const_cast(_gpuObject).requestInterestInMip(_populatedMip - 1); return; } GLuint oldId = _id; diff --git a/libraries/gpu/src/gpu/Texture_ktx.cpp b/libraries/gpu/src/gpu/Texture_ktx.cpp index 9a1c633be5..620fd56277 100644 --- a/libraries/gpu/src/gpu/Texture_ktx.cpp +++ b/libraries/gpu/src/gpu/Texture_ktx.cpp @@ -123,8 +123,9 @@ uint16 KtxStorage::minAvailableMipLevel() const { } void KtxStorage::assignMipData(uint16 level, const storage::StoragePointer& storage) { + qDebug() << "Populating " << level << " " << _filename.c_str(); if (level != _minMipLevelAvailable - 1) { - qWarning() << "Invalid level to be stored, expected: " << (_minMipLevelAvailable - 1) << ", got: " << level; + qWarning() << "Invalid level to be stored, expected: " << (_minMipLevelAvailable - 1) << ", got: " << level << " " << _filename.c_str(); //throw std::runtime_error("Invalid image size for level"); return; } diff --git a/libraries/ktx/src/ktx/Reader.cpp b/libraries/ktx/src/ktx/Reader.cpp index b22f262e85..27aee8771c 100644 --- a/libraries/ktx/src/ktx/Reader.cpp +++ b/libraries/ktx/src/ktx/Reader.cpp @@ -50,7 +50,7 @@ namespace ktx { bool checkIdentifier(const Byte* identifier) { if (!(0 == memcmp(identifier, Header::IDENTIFIER.data(), Header::IDENTIFIER_LENGTH))) { - throw ReaderException("identifier field invalid"); + //throw ReaderException("identifier field invalid"); return false; } return true; diff --git a/libraries/model-networking/src/model-networking/TextureCache.cpp b/libraries/model-networking/src/model-networking/TextureCache.cpp index 17f1d62b7b..84e9c82946 100644 --- a/libraries/model-networking/src/model-networking/TextureCache.cpp +++ b/libraries/model-networking/src/model-networking/TextureCache.cpp @@ -353,7 +353,8 @@ void NetworkTexture::makeRequest() { _ktxHeaderRequest = ResourceManager::createResourceRequest(this, _activeUrl); if (!_ktxHeaderRequest) { - //qCDebug(networking).noquote() << "Failed to get request for" << _url.toDisplayString(); + qCDebug(networking).noquote() << "Failed to get request for" << _url.toDisplayString(); + PROFILE_ASYNC_END(resource, "Resource:" + getType(), QString::number(_requestID)); return; } @@ -424,6 +425,13 @@ void NetworkTexture::startMipRangeRequest(uint16_t low, uint16_t high) { _ktxMipRequest = ResourceManager::createResourceRequest(this, _activeUrl); + if (!_ktxMipRequest) { + qCDebug(networking).noquote() << "Failed to get request for" << _url.toDisplayString(); + + PROFILE_ASYNC_END(resource, "Resource:" + getType(), QString::number(_requestID)); + return; + } + _ktxMipLevelRangeInFlight = { low, high }; if (isHighMipRequest) { // This is a special case where we load the high 7 mips @@ -450,8 +458,8 @@ void NetworkTexture::startMipRangeRequest(uint16_t low, uint16_t high) { void NetworkTexture::ktxHeaderRequestFinished() { Q_ASSERT(_ktxResourceState == LOADING_INITIAL_DATA); -_ktxHeaderRequestFinished = true; -maybeHandleFinishedInitialLoad(); + _ktxHeaderRequestFinished = true; + maybeHandleFinishedInitialLoad(); } void NetworkTexture::ktxMipRequestFinished() { @@ -470,10 +478,11 @@ void NetworkTexture::ktxMipRequestFinished() { auto texture = _textureSource->getGPUTexture(); if (texture) { - _lowestKnownPopulatedMip = _ktxMipLevelRangeInFlight.first; + //_lowestKnownPopulatedMip = _ktxMipLevelRangeInFlight.first; //qDebug() << "Writing mip for " << _url; texture->assignStoredMip(_ktxMipLevelRangeInFlight.first, _ktxMipRequest->getData().size(), reinterpret_cast(_ktxMipRequest->getData().data())); + _lowestKnownPopulatedMip = _textureSource->getGPUTexture()->minAvailableMipLevel(); } else { qWarning(networking) << "Trying to update mips but texture is null"; @@ -529,11 +538,6 @@ void NetworkTexture::maybeHandleFinishedInitialLoad() { auto ktxHeaderData = _ktxHeaderRequest->getData(); auto ktxHighMipData = _ktxMipRequest->getData(); - _ktxHeaderRequest->deleteLater(); - _ktxHeaderRequest = nullptr; - _ktxMipRequest->deleteLater(); - _ktxMipRequest = nullptr; - auto header = reinterpret_cast(ktxHeaderData.data()); qDebug() << "Creating KTX"; @@ -548,10 +552,11 @@ void NetworkTexture::maybeHandleFinishedInitialLoad() { qWarning() << "Cannot load " << _url << ", invalid header identifier"; _ktxResourceState = FAILED_TO_LOAD; finishedLoading(false); + return; } auto kvSize = header->bytesOfKeyValueData; - if (kvSize > ktxHeaderData.size() - ktx::KTX_HEADER_SIZE) { + if (kvSize > (ktxHeaderData.size() - ktx::KTX_HEADER_SIZE)) { qWarning() << "Cannot load " << _url << ", did not receive all kv data with initial request"; _ktxResourceState = FAILED_TO_LOAD; finishedLoading(false); @@ -663,11 +668,18 @@ void NetworkTexture::maybeHandleFinishedInitialLoad() { } } + _lowestKnownPopulatedMip = texture->minAvailableMipLevel(); + texture->registerMipInterestListener(this); _ktxResourceState = WAITING_FOR_MIP_REQUEST; setImage(texture, header->getPixelWidth(), header->getPixelHeight()); qDebug() << "Loaded KTX: " << QString::fromStdString(hash) << " : " << _url; + + _ktxHeaderRequest->deleteLater(); + _ktxHeaderRequest = nullptr; + _ktxMipRequest->deleteLater(); + _ktxMipRequest = nullptr; } } }