fix scattering and working on resource cache bug

This commit is contained in:
SamGondelman 2019-02-05 17:32:15 -08:00
parent bab411e001
commit 3ee448a89a
21 changed files with 199 additions and 105 deletions

View file

@ -36,12 +36,13 @@ AnimationPointer AnimationCache::getAnimation(const QUrl& url) {
return getResource(url).staticCast<Animation>(); return getResource(url).staticCast<Animation>();
} }
QSharedPointer<Resource> AnimationCache::createResource(const QUrl& url, const QSharedPointer<Resource>& fallback, QSharedPointer<Resource> AnimationCache::createResource(const QUrl& url) {
const void* extra) {
return QSharedPointer<Resource>(new Animation(url), &Resource::deleter); return QSharedPointer<Resource>(new Animation(url), &Resource::deleter);
} }
Animation::Animation(const QUrl& url) : Resource(url) {} QSharedPointer<Resource> AnimationCache::createResourceCopy(const QSharedPointer<Resource>& resource) {
return QSharedPointer<Resource>(new Animation(*resource.staticCast<Animation>().data()), &Resource::deleter);
}
AnimationReader::AnimationReader(const QUrl& url, const QByteArray& data) : AnimationReader::AnimationReader(const QUrl& url, const QByteArray& data) :
_url(url), _url(url),

View file

@ -34,9 +34,9 @@ public:
Q_INVOKABLE AnimationPointer getAnimation(const QUrl& url); Q_INVOKABLE AnimationPointer getAnimation(const QUrl& url);
protected: protected:
virtual QSharedPointer<Resource> createResource(const QUrl& url) override;
QSharedPointer<Resource> createResourceCopy(const QSharedPointer<Resource>& resource) override;
virtual QSharedPointer<Resource> createResource(const QUrl& url, const QSharedPointer<Resource>& fallback,
const void* extra) override;
private: private:
explicit AnimationCache(QObject* parent = NULL); explicit AnimationCache(QObject* parent = NULL);
virtual ~AnimationCache() { } virtual ~AnimationCache() { }
@ -62,7 +62,8 @@ class Animation : public Resource {
public: public:
explicit Animation(const QUrl& url); Animation(const Animation& other) : Resource(other), _hfmModel(other._hfmModel) {}
Animation(const QUrl& url) : Resource(url) {}
QString getType() const override { return "Animation"; } QString getType() const override { return "Animation"; }

View file

@ -33,9 +33,12 @@ SharedSoundPointer SoundCache::getSound(const QUrl& url) {
return getResource(url).staticCast<Sound>(); return getResource(url).staticCast<Sound>();
} }
QSharedPointer<Resource> SoundCache::createResource(const QUrl& url, const QSharedPointer<Resource>& fallback, QSharedPointer<Resource> SoundCache::createResource(const QUrl& url) {
const void* extra) {
auto resource = QSharedPointer<Resource>(new Sound(url), &Resource::deleter); auto resource = QSharedPointer<Resource>(new Sound(url), &Resource::deleter);
resource->setLoadPriority(this, SOUNDS_LOADING_PRIORITY); resource->setLoadPriority(this, SOUNDS_LOADING_PRIORITY);
return resource; return resource;
} }
QSharedPointer<Resource> SoundCache::createResourceCopy(const QSharedPointer<Resource>& resource) {
return QSharedPointer<Resource>(new Sound(*resource.staticCast<Sound>().data()), &Resource::deleter);
}

View file

@ -24,8 +24,9 @@ public:
Q_INVOKABLE SharedSoundPointer getSound(const QUrl& url); Q_INVOKABLE SharedSoundPointer getSound(const QUrl& url);
protected: protected:
virtual QSharedPointer<Resource> createResource(const QUrl& url, const QSharedPointer<Resource>& fallback, virtual QSharedPointer<Resource> createResource(const QUrl& url) override;
const void* extra) override; QSharedPointer<Resource> createResourceCopy(const QSharedPointer<Resource>& resource) override;
private: private:
SoundCache(QObject* parent = NULL); SoundCache(QObject* parent = NULL);
}; };

View file

@ -280,11 +280,7 @@ bool RenderableModelEntityItem::findDetailedParabolaIntersection(const glm::vec3
} }
void RenderableModelEntityItem::fetchCollisionGeometryResource() { void RenderableModelEntityItem::fetchCollisionGeometryResource() {
QUrl hullURL(getCollisionShapeURL()); _compoundShapeResource = DependencyManager::get<ModelCache>()->getCollisionGeometryResource(getCollisionShapeURL());
QUrlQuery queryArgs(hullURL);
queryArgs.addQueryItem("collision-hull", "");
hullURL.setQuery(queryArgs);
_compoundShapeResource = DependencyManager::get<ModelCache>()->getCollisionGeometryResource(hullURL);
} }
bool RenderableModelEntityItem::computeShapeFailedToLoad() { bool RenderableModelEntityItem::computeShapeFailedToLoad() {

View file

@ -76,13 +76,12 @@ HFMTexture FBXSerializer::getTexture(const QString& textureID, const QString& ma
} }
void FBXSerializer::consolidateHFMMaterials(const QVariantHash& mapping) { void FBXSerializer::consolidateHFMMaterials(const QVariantHash& mapping) {
QJsonObject materialMap;
QString materialMapString = mapping.value("materialMap").toString(); if (mapping.contains("materialMap")) {
QJsonDocument materialMapDocument = QJsonDocument::fromJson(materialMapString.toUtf8()); QByteArray materialMapValue = mapping.value("materialMap").toByteArray();
QJsonObject materialMap = materialMapDocument.object(); materialMap = QJsonDocument::fromJson(materialMapValue).object();
if (!materialMapString.isEmpty()) { if (materialMap.isEmpty()) {
if (materialMapDocument.isEmpty() || materialMap.isEmpty()) { qCDebug(modelformat) << "fbx Material Map found but did not produce valid JSON:" << materialMapValue;
qCDebug(modelformat) << "fbx Material Map found but did not produce valid JSON:" << materialMapString;
} }
} }
for (QHash<QString, HFMMaterial>::iterator it = _hfmMaterials.begin(); it != _hfmMaterials.end(); it++) { for (QHash<QString, HFMMaterial>::iterator it = _hfmMaterials.begin(); it != _hfmMaterials.end(); it++) {

View file

@ -88,7 +88,7 @@ QByteArray FSTReader::writeMapping(const QVariantHash& mapping) {
<< BLENDSHAPE_FIELD << JOINT_INDEX_FIELD; << BLENDSHAPE_FIELD << JOINT_INDEX_FIELD;
QBuffer buffer; QBuffer buffer;
buffer.open(QIODevice::WriteOnly); buffer.open(QIODevice::WriteOnly);
for (auto key : PREFERED_ORDER) { for (auto key : PREFERED_ORDER) {
auto it = mapping.find(key); auto it = mapping.find(key);
if (it != mapping.constEnd()) { if (it != mapping.constEnd()) {
@ -104,7 +104,7 @@ QByteArray FSTReader::writeMapping(const QVariantHash& mapping) {
} }
} }
} }
for (auto it = mapping.constBegin(); it != mapping.constEnd(); it++) { for (auto it = mapping.constBegin(); it != mapping.constEnd(); it++) {
if (!PREFERED_ORDER.contains(it.key())) { if (!PREFERED_ORDER.contains(it.key())) {
writeVariant(buffer, it); writeVariant(buffer, it);

View file

@ -105,7 +105,7 @@ void Material::setMetallic(float metallic) {
void Material::setScattering(float scattering) { void Material::setScattering(float scattering) {
scattering = glm::clamp(scattering, 0.0f, 1.0f); scattering = glm::clamp(scattering, 0.0f, 1.0f);
_key.setMetallic(scattering > 0.0f); _key.setScattering(scattering > 0.0f);
_scattering = scattering; _scattering = scattering;
} }

View file

@ -417,9 +417,13 @@ MaterialCache& MaterialCache::instance() {
} }
NetworkMaterialResourcePointer MaterialCache::getMaterial(const QUrl& url) { NetworkMaterialResourcePointer MaterialCache::getMaterial(const QUrl& url) {
return ResourceCache::getResource(url, QUrl(), nullptr).staticCast<NetworkMaterialResource>(); return ResourceCache::getResource(url, QUrl()).staticCast<NetworkMaterialResource>();
} }
QSharedPointer<Resource> MaterialCache::createResource(const QUrl& url, const QSharedPointer<Resource>& fallback, const void* extra) { QSharedPointer<Resource> MaterialCache::createResource(const QUrl& url) {
return QSharedPointer<Resource>(new NetworkMaterialResource(url), &Resource::deleter); return QSharedPointer<Resource>(new NetworkMaterialResource(url), &Resource::deleter);
}
QSharedPointer<Resource> MaterialCache::createResourceCopy(const QSharedPointer<Resource>& resource) {
return QSharedPointer<Resource>(new NetworkMaterialResource(*resource.staticCast<NetworkMaterialResource>().data()), &Resource::deleter);
} }

View file

@ -53,7 +53,8 @@ public:
NetworkMaterialResourcePointer getMaterial(const QUrl& url); NetworkMaterialResourcePointer getMaterial(const QUrl& url);
protected: protected:
virtual QSharedPointer<Resource> createResource(const QUrl& url, const QSharedPointer<Resource>& fallback, const void* extra) override; virtual QSharedPointer<Resource> createResource(const QUrl& url) override;
QSharedPointer<Resource> createResourceCopy(const QSharedPointer<Resource>& resource) override;
}; };
#endif #endif

View file

@ -107,7 +107,7 @@ void GeometryMappingResource::downloadFinished(const QByteArray& data) {
} }
auto modelCache = DependencyManager::get<ModelCache>(); auto modelCache = DependencyManager::get<ModelCache>();
GeometryExtra extra{ _mapping, _textureBaseUrl, false }; GeometryExtra extra { _mapping, _textureBaseUrl, false };
// Get the raw GeometryResource // Get the raw GeometryResource
_geometryResource = modelCache->getResource(url, QUrl(), &extra).staticCast<GeometryResource>(); _geometryResource = modelCache->getResource(url, QUrl(), &extra).staticCast<GeometryResource>();
@ -253,13 +253,19 @@ void GeometryReader::run() {
class GeometryDefinitionResource : public GeometryResource { class GeometryDefinitionResource : public GeometryResource {
Q_OBJECT Q_OBJECT
public: public:
GeometryDefinitionResource(const ModelLoader& modelLoader, const QUrl& url, const QVariantHash& mapping, const QUrl& textureBaseUrl, bool combineParts) : GeometryDefinitionResource(const ModelLoader& modelLoader, const QUrl& url) : GeometryResource(url), _modelLoader(modelLoader) {}
GeometryResource(url, resolveTextureBaseUrl(url, textureBaseUrl)), _modelLoader(modelLoader), _mapping(mapping), _combineParts(combineParts) {} GeometryDefinitionResource(const GeometryDefinitionResource& other) :
GeometryResource(other),
_modelLoader(other._modelLoader),
_mapping(other._mapping),
_combineParts(other._combineParts) {}
QString getType() const override { return "GeometryDefinition"; } QString getType() const override { return "GeometryDefinition"; }
virtual void downloadFinished(const QByteArray& data) override; virtual void downloadFinished(const QByteArray& data) override;
void setExtra(void* extra) override;
protected: protected:
Q_INVOKABLE void setGeometryDefinition(HFMModel::Pointer hfmModel); Q_INVOKABLE void setGeometryDefinition(HFMModel::Pointer hfmModel);
@ -269,6 +275,13 @@ private:
bool _combineParts; bool _combineParts;
}; };
void GeometryDefinitionResource::setExtra(void* extra) {
const GeometryExtra* geometryExtra = static_cast<const GeometryExtra*>(extra);
_mapping = geometryExtra ? geometryExtra->mapping : QVariantHash();
_textureBaseUrl = resolveTextureBaseUrl(_url, geometryExtra ? geometryExtra->textureBaseUrl : QUrl());
_combineParts = geometryExtra ? geometryExtra->combineParts : true;
}
void GeometryDefinitionResource::downloadFinished(const QByteArray& data) { void GeometryDefinitionResource::downloadFinished(const QByteArray& data) {
if (_url != _effectiveBaseURL) { if (_url != _effectiveBaseURL) {
_url = _effectiveBaseURL; _url = _effectiveBaseURL;
@ -323,22 +336,21 @@ ModelCache::ModelCache() {
modelFormatRegistry->addFormat(GLTFSerializer()); modelFormatRegistry->addFormat(GLTFSerializer());
} }
QSharedPointer<Resource> ModelCache::createResource(const QUrl& url, const QSharedPointer<Resource>& fallback, QSharedPointer<Resource> ModelCache::createResource(const QUrl& url) {
const void* extra) {
Resource* resource = nullptr; Resource* resource = nullptr;
if (url.path().toLower().endsWith(".fst")) { if (url.path().toLower().endsWith(".fst")) {
resource = new GeometryMappingResource(url); resource = new GeometryMappingResource(url);
} else { } else {
const GeometryExtra* geometryExtra = static_cast<const GeometryExtra*>(extra); resource = new GeometryDefinitionResource(_modelLoader, url);
auto mapping = geometryExtra ? geometryExtra->mapping : QVariantHash();
auto textureBaseUrl = geometryExtra ? geometryExtra->textureBaseUrl : QUrl();
bool combineParts = geometryExtra ? geometryExtra->combineParts : true;
resource = new GeometryDefinitionResource(_modelLoader, url, mapping, textureBaseUrl, combineParts);
} }
return QSharedPointer<Resource>(resource, &Resource::deleter); return QSharedPointer<Resource>(resource, &Resource::deleter);
} }
QSharedPointer<Resource> ModelCache::createResourceCopy(const QSharedPointer<Resource>& resource) {
return QSharedPointer<Resource>(new GeometryDefinitionResource(*resource.staticCast<GeometryDefinitionResource>().data()), &Resource::deleter);
}
GeometryResource::Pointer ModelCache::getGeometryResource(const QUrl& url, GeometryResource::Pointer ModelCache::getGeometryResource(const QUrl& url,
const QVariantHash& mapping, const QUrl& textureBaseUrl) { const QVariantHash& mapping, const QUrl& textureBaseUrl) {
bool combineParts = true; bool combineParts = true;

View file

@ -82,8 +82,12 @@ class GeometryResource : public Resource, public Geometry {
public: public:
using Pointer = QSharedPointer<GeometryResource>; using Pointer = QSharedPointer<GeometryResource>;
GeometryResource(const QUrl& url, const QUrl& textureBaseUrl = QUrl()) : GeometryResource(const QUrl& url) : Resource(url) {}
Resource(url), _textureBaseUrl(textureBaseUrl) {} GeometryResource(const GeometryResource& other) :
Resource(other),
Geometry(other),
_textureBaseUrl(other._textureBaseUrl),
_isCacheable(other._isCacheable) {}
virtual bool areTexturesLoaded() const override { return isLoaded() && Geometry::areTexturesLoaded(); } virtual bool areTexturesLoaded() const override { return isLoaded() && Geometry::areTexturesLoaded(); }
@ -153,8 +157,8 @@ public:
protected: protected:
friend class GeometryMappingResource; friend class GeometryMappingResource;
virtual QSharedPointer<Resource> createResource(const QUrl& url, const QSharedPointer<Resource>& fallback, virtual QSharedPointer<Resource> createResource(const QUrl& url) override;
const void* extra) override; QSharedPointer<Resource> createResourceCopy(const QSharedPointer<Resource>& resource) override;
private: private:
ModelCache(); ModelCache();

View file

@ -21,11 +21,13 @@ ShaderCache& ShaderCache::instance() {
} }
NetworkShaderPointer ShaderCache::getShader(const QUrl& url) { NetworkShaderPointer ShaderCache::getShader(const QUrl& url) {
return ResourceCache::getResource(url, QUrl(), nullptr).staticCast<NetworkShader>(); return ResourceCache::getResource(url, QUrl()).staticCast<NetworkShader>();
} }
QSharedPointer<Resource> ShaderCache::createResource(const QUrl& url, const QSharedPointer<Resource>& fallback, QSharedPointer<Resource> ShaderCache::createResource(const QUrl& url) {
const void* extra) {
return QSharedPointer<Resource>(new NetworkShader(url), &Resource::deleter); return QSharedPointer<Resource>(new NetworkShader(url), &Resource::deleter);
} }
QSharedPointer<Resource> ShaderCache::createResourceCopy(const QSharedPointer<Resource>& resource) {
return QSharedPointer<Resource>(new NetworkShader(*resource.staticCast<NetworkShader>().data()), &Resource::deleter);
}

View file

@ -14,6 +14,7 @@
class NetworkShader : public Resource { class NetworkShader : public Resource {
public: public:
NetworkShader(const QUrl& url); NetworkShader(const QUrl& url);
NetworkShader(const NetworkShader& other) : Resource(other), _source(other._source) {}
QString getType() const override { return "NetworkShader"; } QString getType() const override { return "NetworkShader"; }
@ -31,8 +32,8 @@ public:
NetworkShaderPointer getShader(const QUrl& url); NetworkShaderPointer getShader(const QUrl& url);
protected: protected:
virtual QSharedPointer<Resource> createResource(const QUrl& url, const QSharedPointer<Resource>& fallback, virtual QSharedPointer<Resource> createResource(const QUrl& url) override;
const void* extra) override; QSharedPointer<Resource> createResourceCopy(const QSharedPointer<Resource>& resource) override;
}; };
#endif #endif

View file

@ -305,42 +305,52 @@ gpu::TexturePointer TextureCache::getImageTexture(const QString& path, image::Te
return gpu::TexturePointer(loader(std::move(image), path.toStdString(), shouldCompress, target, false)); return gpu::TexturePointer(loader(std::move(image), path.toStdString(), shouldCompress, target, false));
} }
QSharedPointer<Resource> TextureCache::createResource(const QUrl& url, const QSharedPointer<Resource>& fallback, QSharedPointer<Resource> TextureCache::createResource(const QUrl& url) {
const void* extra) { return QSharedPointer<Resource>(new NetworkTexture(url), &Resource::deleter);
const TextureExtra* textureExtra = static_cast<const TextureExtra*>(extra); }
auto type = textureExtra ? textureExtra->type : image::TextureUsage::DEFAULT_TEXTURE;
auto content = textureExtra ? textureExtra->content : QByteArray(); QSharedPointer<Resource> TextureCache::createResourceCopy(const QSharedPointer<Resource>& resource) {
auto maxNumPixels = textureExtra ? textureExtra->maxNumPixels : ABSOLUTE_MAX_TEXTURE_NUM_PIXELS; return QSharedPointer<Resource>(new NetworkTexture(*resource.staticCast<NetworkTexture>().data()), &Resource::deleter);
NetworkTexture* texture = new NetworkTexture(url, type, content, maxNumPixels);
return QSharedPointer<Resource>(texture, &Resource::deleter);
} }
int networkTexturePointerMetaTypeId = qRegisterMetaType<QWeakPointer<NetworkTexture>>(); int networkTexturePointerMetaTypeId = qRegisterMetaType<QWeakPointer<NetworkTexture>>();
NetworkTexture::NetworkTexture(const QUrl& url) : NetworkTexture::NetworkTexture(const QUrl& url) :
Resource(url), Resource(url),
_type(), _type(),
_maxNumPixels(100) _maxNumPixels(100)
{ {
_textureSource = std::make_shared<gpu::TextureSource>(url); _textureSource = std::make_shared<gpu::TextureSource>(url);
_lowestRequestedMipLevel = 0; _lowestRequestedMipLevel = 0;
_loaded = true; _loaded = true;
} }
NetworkTexture::NetworkTexture(const NetworkTexture& other) :
Resource(other),
_type(other._type),
_currentlyLoadingResourceType(other._currentlyLoadingResourceType),
_originalWidth(other._originalWidth),
_originalHeight(other._originalHeight),
_width(other._width),
_height(other._height),
_maxNumPixels(other._maxNumPixels)
{
}
static bool isLocalUrl(const QUrl& url) { static bool isLocalUrl(const QUrl& url) {
auto scheme = url.scheme(); auto scheme = url.scheme();
return (scheme == HIFI_URL_SCHEME_FILE || scheme == URL_SCHEME_QRC || scheme == RESOURCE_SCHEME); return (scheme == HIFI_URL_SCHEME_FILE || scheme == URL_SCHEME_QRC || scheme == RESOURCE_SCHEME);
} }
NetworkTexture::NetworkTexture(const QUrl& url, image::TextureUsage::Type type, const QByteArray& content, int maxNumPixels) : void NetworkTexture::setExtra(void* extra) {
Resource(url), const TextureExtra* textureExtra = static_cast<const TextureExtra*>(extra);
_type(type), _type = textureExtra ? textureExtra->type : image::TextureUsage::DEFAULT_TEXTURE;
_maxNumPixels(maxNumPixels) _maxNumPixels = textureExtra ? textureExtra->maxNumPixels : ABSOLUTE_MAX_TEXTURE_NUM_PIXELS;
{
_textureSource = std::make_shared<gpu::TextureSource>(url, (int)type); _textureSource = std::make_shared<gpu::TextureSource>(_url, (int)_type);
_lowestRequestedMipLevel = 0; _lowestRequestedMipLevel = 0;
auto fileNameLowercase = url.fileName().toLower(); auto fileNameLowercase = _url.fileName().toLower();
if (fileNameLowercase.endsWith(TEXTURE_META_EXTENSION)) { if (fileNameLowercase.endsWith(TEXTURE_META_EXTENSION)) {
_currentlyLoadingResourceType = ResourceType::META; _currentlyLoadingResourceType = ResourceType::META;
} else if (fileNameLowercase.endsWith(".ktx")) { } else if (fileNameLowercase.endsWith(".ktx")) {
@ -351,17 +361,18 @@ NetworkTexture::NetworkTexture(const QUrl& url, image::TextureUsage::Type type,
_shouldFailOnRedirect = _currentlyLoadingResourceType != ResourceType::KTX; _shouldFailOnRedirect = _currentlyLoadingResourceType != ResourceType::KTX;
if (type == image::TextureUsage::CUBE_TEXTURE) { if (_type == image::TextureUsage::CUBE_TEXTURE) {
setLoadPriority(this, SKYBOX_LOAD_PRIORITY); setLoadPriority(this, SKYBOX_LOAD_PRIORITY);
} else if (_currentlyLoadingResourceType == ResourceType::KTX) { } else if (_currentlyLoadingResourceType == ResourceType::KTX) {
setLoadPriority(this, HIGH_MIPS_LOAD_PRIORITY); setLoadPriority(this, HIGH_MIPS_LOAD_PRIORITY);
} }
if (!url.isValid()) { if (!_url.isValid()) {
_loaded = true; _loaded = true;
} }
// if we have content, load it after we have our self pointer // if we have content, load it after we have our self pointer
auto content = textureExtra ? textureExtra->content : QByteArray();
if (!content.isEmpty()) { if (!content.isEmpty()) {
_startedLoading = true; _startedLoading = true;
QMetaObject::invokeMethod(this, "downloadFinished", Qt::QueuedConnection, Q_ARG(const QByteArray&, content)); QMetaObject::invokeMethod(this, "downloadFinished", Qt::QueuedConnection, Q_ARG(const QByteArray&, content));

View file

@ -46,7 +46,7 @@ class NetworkTexture : public Resource, public Texture {
public: public:
NetworkTexture(const QUrl& url); NetworkTexture(const QUrl& url);
NetworkTexture(const QUrl& url, image::TextureUsage::Type type, const QByteArray& content, int maxNumPixels); NetworkTexture(const NetworkTexture& other);
~NetworkTexture() override; ~NetworkTexture() override;
QString getType() const override { return "NetworkTexture"; } QString getType() const override { return "NetworkTexture"; }
@ -63,6 +63,8 @@ public:
Q_INVOKABLE void setOriginalDescriptor(ktx::KTXDescriptor* descriptor) { _originalKtxDescriptor.reset(descriptor); } Q_INVOKABLE void setOriginalDescriptor(ktx::KTXDescriptor* descriptor) { _originalKtxDescriptor.reset(descriptor); }
void setExtra(void* extra) override;
signals: signals:
void networkTextureCreated(const QWeakPointer<NetworkTexture>& self); void networkTextureCreated(const QWeakPointer<NetworkTexture>& self);
@ -201,8 +203,8 @@ protected:
// Overload ResourceCache::prefetch to allow specifying texture type for loads // Overload ResourceCache::prefetch to allow specifying texture type for loads
Q_INVOKABLE ScriptableResource* prefetch(const QUrl& url, int type, int maxNumPixels = ABSOLUTE_MAX_TEXTURE_NUM_PIXELS); Q_INVOKABLE ScriptableResource* prefetch(const QUrl& url, int type, int maxNumPixels = ABSOLUTE_MAX_TEXTURE_NUM_PIXELS);
virtual QSharedPointer<Resource> createResource(const QUrl& url, const QSharedPointer<Resource>& fallback, virtual QSharedPointer<Resource> createResource(const QUrl& url) override;
const void* extra) override; QSharedPointer<Resource> createResourceCopy(const QSharedPointer<Resource>& resource) override;
private: private:
friend class ImageReader; friend class ImageReader;

View file

@ -265,16 +265,17 @@ ResourceCache::~ResourceCache() {
void ResourceCache::clearATPAssets() { void ResourceCache::clearATPAssets() {
{ {
QWriteLocker locker(&_resourcesLock); QWriteLocker locker(&_resourcesLock);
for (auto& url : _resources.keys()) { QList<QUrl> urls = _resources.keys();
for (auto& url : urls) {
// If this is an ATP resource // If this is an ATP resource
if (url.scheme() == URL_SCHEME_ATP) { if (url.scheme() == URL_SCHEME_ATP) {
auto resourcesWithExtraHash = _resources.take(url);
// Remove it from the resource hash for (auto& resource : resourcesWithExtraHash) {
auto resource = _resources.take(url); if (auto strongRef = resource.lock()) {
if (auto strongRef = resource.lock()) { // Make sure the resource won't reinsert itself
// Make sure the resource won't reinsert itself strongRef->setCache(nullptr);
strongRef->setCache(nullptr); _totalResourcesSize -= strongRef->getBytes();
_totalResourcesSize -= strongRef->getBytes(); }
} }
} }
} }
@ -297,16 +298,20 @@ void ResourceCache::refreshAll() {
clearUnusedResources(); clearUnusedResources();
resetUnusedResourceCounter(); resetUnusedResourceCounter();
QHash<QUrl, QWeakPointer<Resource>> resources; QHash<QUrl, QHash<int, QWeakPointer<Resource>>> allResources;
{ {
QReadLocker locker(&_resourcesLock); QReadLocker locker(&_resourcesLock);
resources = _resources; allResources = _resources;
} }
// Refresh all remaining resources in use // Refresh all remaining resources in use
foreach (QSharedPointer<Resource> resource, resources) { // FIXME: this will trigger multiple refreshes for the same resource if they have different hashes
if (resource) { for (auto& resourcesWithExtraHash : allResources) {
resource->refresh(); for (auto& resourceWeak : resourcesWithExtraHash) {
auto resource = resourceWeak.lock();
if (resource) {
resource->refresh();
}
} }
} }
} }
@ -338,39 +343,59 @@ void ResourceCache::setRequestLimit(uint32_t limit) {
} }
} }
QSharedPointer<Resource> ResourceCache::getResource(const QUrl& url, const QUrl& fallback, void* extra) { QSharedPointer<Resource> ResourceCache::getResource(const QUrl& url, const QUrl& fallback, void* extra, int extraHash) {
QSharedPointer<Resource> resource; QSharedPointer<Resource> resource;
if (extra && extraHash < 0) {
qDebug() << "ResourceCache::getResource: ERROR! Non-null extra, but invalid extraHash";
return resource;
}
{ {
QReadLocker locker(&_resourcesLock); QReadLocker locker(&_resourcesLock);
resource = _resources.value(url).lock(); auto& resourcesWithExtraHash = _resources[url];
auto resourcesWithExtraHashIter = resourcesWithExtraHash.find(extraHash);
if (resourcesWithExtraHashIter != resourcesWithExtraHash.end()) {
// We've seen this extra info before
resource = resourcesWithExtraHashIter.value().lock();
} else if (resourcesWithExtraHash.size() > 0.0f) {
// We haven't seen this extra info before, but we've already downloaded the resource. We need a new copy of this object (with any old hash).
resource = createResourceCopy(resourcesWithExtraHash.begin().value().lock());
resource->setExtra(extra);
resource->setExtraHash(extraHash);
resource->setSelf(resource);
resource->setCache(this);
resource->moveToThread(qApp->thread());
connect(resource.data(), &Resource::updateSize, this, &ResourceCache::updateTotalSize);
resourcesWithExtraHash.insert(extraHash, resource);
removeUnusedResource(resource);
resource->ensureLoading();
}
} }
if (resource) { if (resource) {
removeUnusedResource(resource); removeUnusedResource(resource);
} }
if (!resource && !url.isValid() && !url.isEmpty() && fallback.isValid()) { if (!resource && (!url.isValid() || url.isEmpty()) && fallback.isValid()) {
resource = getResource(fallback, QUrl()); resource = getResource(fallback, QUrl(), extra, extraHash);
} }
if (!resource) { if (!resource) {
resource = createResource( resource = createResource(url);
url, resource->setExtra(extra);
fallback.isValid() ? getResource(fallback, QUrl()) : QSharedPointer<Resource>(), resource->setExtraHash(extraHash);
extra);
resource->setSelf(resource); resource->setSelf(resource);
resource->setCache(this); resource->setCache(this);
resource->moveToThread(qApp->thread()); resource->moveToThread(qApp->thread());
connect(resource.data(), &Resource::updateSize, this, &ResourceCache::updateTotalSize); connect(resource.data(), &Resource::updateSize, this, &ResourceCache::updateTotalSize);
{ {
QWriteLocker locker(&_resourcesLock); QWriteLocker locker(&_resourcesLock);
_resources.insert(url, resource); _resources[url].insert(extraHash, resource);
} }
removeUnusedResource(resource); removeUnusedResource(resource);
resource->ensureLoading(); resource->ensureLoading();
} }
DependencyManager::get<ResourceRequestObserver>()->update( DependencyManager::get<ResourceRequestObserver>()->update(resource->getURL(), -1, "ResourceCache::getResource");
resource->getURL(), -1, "ResourceCache::getResource");
return resource; return resource;
} }
@ -527,6 +552,26 @@ bool ResourceCache::attemptHighestPriorityRequest() {
static int requestID = 0; static int requestID = 0;
Resource::Resource(const Resource& other) :
_url(other._url),
_extraHash(other._extraHash),
_effectiveBaseURL(other._effectiveBaseURL),
_activeUrl(other._activeUrl),
_requestByteRange(other._requestByteRange),
_shouldFailOnRedirect(other._shouldFailOnRedirect),
_startedLoading(other._startedLoading),
_failedToLoad(other._failedToLoad),
_loaded(other._loaded),
_loadPriorities(other._loadPriorities),
_bytesReceived(other._bytesReceived),
_bytesTotal(other._bytesTotal),
_bytes(other._bytes),
_requestID(++requestID) {
if (!other._loaded) {
_startedLoading = false;
}
}
Resource::Resource(const QUrl& url) : Resource::Resource(const QUrl& url) :
_url(url), _url(url),
_activeUrl(url), _activeUrl(url),
@ -678,7 +723,7 @@ void Resource::setSize(const qint64& bytes) {
void Resource::reinsert() { void Resource::reinsert() {
QWriteLocker locker(&_cache->_resourcesLock); QWriteLocker locker(&_cache->_resourcesLock);
_cache->_resources.insert(_url, _self); _cache->_resources[_url].insert(_extraHash, _self);
} }

View file

@ -239,8 +239,7 @@ protected slots:
/// returns an empty smart pointer and loads its asynchronously. /// returns an empty smart pointer and loads its asynchronously.
/// \param fallback a fallback URL to load if the desired one is unavailable /// \param fallback a fallback URL to load if the desired one is unavailable
/// \param extra extra data to pass to the creator, if appropriate /// \param extra extra data to pass to the creator, if appropriate
QSharedPointer<Resource> getResource(const QUrl& url, const QUrl& fallback = QUrl(), QSharedPointer<Resource> getResource(const QUrl& url, const QUrl& fallback = QUrl(), void* extra = NULL, int extraHash = -1);
void* extra = NULL);
private slots: private slots:
void clearATPAssets(); void clearATPAssets();
@ -254,8 +253,8 @@ protected:
Q_INVOKABLE ScriptableResource* prefetch(const QUrl& url) { return prefetch(url, nullptr); } Q_INVOKABLE ScriptableResource* prefetch(const QUrl& url) { return prefetch(url, nullptr); }
/// Creates a new resource. /// Creates a new resource.
virtual QSharedPointer<Resource> createResource(const QUrl& url, const QSharedPointer<Resource>& fallback, virtual QSharedPointer<Resource> createResource(const QUrl& url) = 0;
const void* extra) = 0; virtual QSharedPointer<Resource> createResourceCopy(const QSharedPointer<Resource>& resource) = 0;
void addUnusedResource(const QSharedPointer<Resource>& resource); void addUnusedResource(const QSharedPointer<Resource>& resource);
void removeUnusedResource(const QSharedPointer<Resource>& resource); void removeUnusedResource(const QSharedPointer<Resource>& resource);
@ -278,7 +277,7 @@ private:
void resetResourceCounters(); void resetResourceCounters();
// Resources // Resources
QHash<QUrl, QWeakPointer<Resource>> _resources; QHash<QUrl, QHash<int, QWeakPointer<Resource>>> _resources;
QReadWriteLock _resourcesLock { QReadWriteLock::Recursive }; QReadWriteLock _resourcesLock { QReadWriteLock::Recursive };
int _lastLRUKey = 0; int _lastLRUKey = 0;
@ -359,7 +358,8 @@ class Resource : public QObject {
Q_OBJECT Q_OBJECT
public: public:
Resource(const Resource& other);
Resource(const QUrl& url); Resource(const QUrl& url);
virtual ~Resource(); virtual ~Resource();
@ -415,6 +415,9 @@ public:
unsigned int getDownloadAttempts() { return _attempts; } unsigned int getDownloadAttempts() { return _attempts; }
unsigned int getDownloadAttemptsRemaining() { return _attemptsRemaining; } unsigned int getDownloadAttemptsRemaining() { return _attemptsRemaining; }
virtual void setExtra(void* extra) {};
void setExtraHash(int extraHash) { _extraHash = extraHash; }
signals: signals:
/// Fired when the resource begins downloading. /// Fired when the resource begins downloading.
void loading(); void loading();
@ -469,7 +472,7 @@ protected:
virtual bool handleFailedRequest(ResourceRequest::Result result); virtual bool handleFailedRequest(ResourceRequest::Result result);
QUrl _url; QUrl _url;
QUrl _effectiveBaseURL{ _url }; QUrl _effectiveBaseURL { _url };
QUrl _activeUrl; QUrl _activeUrl;
ByteRange _requestByteRange; ByteRange _requestByteRange;
bool _shouldFailOnRedirect { false }; bool _shouldFailOnRedirect { false };
@ -492,6 +495,8 @@ protected:
int _requestID; int _requestID;
ResourceRequest* _request{ nullptr }; ResourceRequest* _request{ nullptr };
int _extraHash { -1 };
public slots: public slots:
void handleDownloadProgress(uint64_t bytesReceived, uint64_t bytesTotal); void handleDownloadProgress(uint64_t bytesReceived, uint64_t bytesTotal);
void handleReplyFinished(); void handleReplyFinished();

View file

@ -48,8 +48,11 @@ NetworkClipLoaderPointer ClipCache::getClipLoader(const QUrl& url) {
return getResource(url).staticCast<NetworkClipLoader>(); return getResource(url).staticCast<NetworkClipLoader>();
} }
QSharedPointer<Resource> ClipCache::createResource(const QUrl& url, const QSharedPointer<Resource>& fallback, const void* extra) { QSharedPointer<Resource> ClipCache::createResource(const QUrl& url) {
qCDebug(recordingLog) << "Loading recording at" << url; qCDebug(recordingLog) << "Loading recording at" << url;
return QSharedPointer<Resource>(new NetworkClipLoader(url), &Resource::deleter); return QSharedPointer<Resource>(new NetworkClipLoader(url), &Resource::deleter);
} }
QSharedPointer<Resource> ClipCache::createResourceCopy(const QSharedPointer<Resource>& resource) {
return QSharedPointer<Resource>(new NetworkClipLoader(*resource.staticCast<NetworkClipLoader>().data()), &Resource::deleter);
}

View file

@ -33,6 +33,8 @@ class NetworkClipLoader : public Resource {
Q_OBJECT Q_OBJECT
public: public:
NetworkClipLoader(const QUrl& url); NetworkClipLoader(const QUrl& url);
NetworkClipLoader(const NetworkClipLoader& other) : Resource(other), _clip(other._clip) {}
virtual void downloadFinished(const QByteArray& data) override; virtual void downloadFinished(const QByteArray& data) override;
ClipPointer getClip() { return _clip; } ClipPointer getClip() { return _clip; }
bool completed() { return _failedToLoad || isLoaded(); } bool completed() { return _failedToLoad || isLoaded(); }
@ -54,7 +56,8 @@ public slots:
NetworkClipLoaderPointer getClipLoader(const QUrl& url); NetworkClipLoaderPointer getClipLoader(const QUrl& url);
protected: protected:
virtual QSharedPointer<Resource> createResource(const QUrl& url, const QSharedPointer<Resource>& fallback, const void* extra) override; virtual QSharedPointer<Resource> createResource(const QUrl& url) override;
QSharedPointer<Resource> createResourceCopy(const QSharedPointer<Resource>& resource) override;
private: private:
ClipCache(QObject* parent = nullptr); ClipCache(QObject* parent = nullptr);

View file

@ -579,7 +579,7 @@ void RenderPipelines::updateMultiMaterial(graphics::MultiMaterial& multiMaterial
} else { } else {
forceDefault = true; forceDefault = true;
} }
schemaKey.setScattering(true); schemaKey.setScatteringMap(true);
} }
break; break;
case graphics::MaterialKey::EMISSIVE_MAP_BIT: case graphics::MaterialKey::EMISSIVE_MAP_BIT: