Allow disabling of KTX cache

This commit is contained in:
Brad Davis 2018-01-13 11:32:47 -08:00
parent 17899d5e07
commit 6d45238bbd
2 changed files with 14 additions and 1 deletions

View file

@ -50,8 +50,10 @@ Q_LOGGING_CATEGORY(trace_resource_parse_image, "trace.resource.parse.image")
Q_LOGGING_CATEGORY(trace_resource_parse_image_raw, "trace.resource.parse.image.raw")
Q_LOGGING_CATEGORY(trace_resource_parse_image_ktx, "trace.resource.parse.image.ktx")
#if ENABLE_KTX_CACHE
const std::string TextureCache::KTX_DIRNAME { "ktx_cache" };
const std::string TextureCache::KTX_EXT { "ktx" };
#endif
static const QString RESOURCE_SCHEME = "resource";
static const QUrl SPECTATOR_CAMERA_FRAME_URL("resource://spectatorCameraFrame");
@ -61,7 +63,9 @@ static const float SKYBOX_LOAD_PRIORITY { 10.0f }; // Make sure skybox loads fir
static const float HIGH_MIPS_LOAD_PRIORITY { 9.0f }; // Make sure high mips loads after skybox but before models
TextureCache::TextureCache() {
#if ENABLE_KTX_CACHE
_ktxCache->initialize();
#endif
setUnusedResourceCacheSize(0);
setObjectName("TextureCache");
}
@ -742,6 +746,7 @@ void NetworkTexture::handleFinishedInitialLoad() {
gpu::TexturePointer texture = textureCache->getTextureByHash(hash);
#if ENABLE_KTX_CACHE
if (!texture) {
auto ktxFile = textureCache->_ktxCache->getFile(hash);
if (ktxFile) {
@ -808,6 +813,7 @@ void NetworkTexture::handleFinishedInitialLoad() {
// be the winner
texture = textureCache->cacheTextureByHash(filename, texture);
}
#endif
QMetaObject::invokeMethod(resource.data(), "setImage",
Q_ARG(gpu::TexturePointer, texture),
@ -927,6 +933,7 @@ void ImageReader::read() {
// If we already have a live texture with the same hash, use it
auto texture = textureCache->getTextureByHash(hash);
#if ENABLE_KTX_CACHE
// If there is no live texture, check if there's an existing KTX file
if (!texture) {
auto ktxFile = textureCache->_ktxCache->getFile(hash);
@ -939,6 +946,7 @@ void ImageReader::read() {
}
}
}
#endif
// If we found the texture either because it's in use or via KTX deserialization,
// set the image and return immediately.
@ -974,6 +982,7 @@ void ImageReader::read() {
// Save the image into a KTXFile
if (texture && textureCache) {
#if ENABLE_KTX_CACHE
auto memKtx = gpu::Texture::serialize(*texture);
// Move the texture into a memory mapped file
@ -990,7 +999,7 @@ void ImageReader::read() {
} else {
qCWarning(modelnetworking) << "Unable to serialize texture to KTX " << _url;
}
#endif
// We replace the texture with the one stored in the cache. This deals with the possible race condition of two different
// images with the same hash being loaded concurrently. Only one of them will make it into the cache by hash first and will
// be the winner

View file

@ -27,6 +27,8 @@
#include "KTXCache.h"
#define ENABLE_KTX_CACHE 0
namespace gpu {
class Batch;
}
@ -194,10 +196,12 @@ private:
TextureCache();
virtual ~TextureCache();
#if ENABLE_KTX_CACHE
static const std::string KTX_DIRNAME;
static const std::string KTX_EXT;
std::shared_ptr<cache::FileCache> _ktxCache { std::make_shared<KTXCache>(KTX_DIRNAME, KTX_EXT) };
#endif
// Map from image hashes to texture weak pointers
std::unordered_map<std::string, std::weak_ptr<gpu::Texture>> _texturesByHashes;
std::mutex _texturesByHashesMutex;