Merge pull request #8704 from ZappoMan/calculateMaterialSizeFixes

make calculateTextureInfo thread safe
This commit is contained in:
samcake 2016-10-01 17:15:39 -07:00 committed by GitHub
commit 964f766427
5 changed files with 62 additions and 42 deletions

View file

@ -44,8 +44,11 @@ Material::Material(const Material& material) :
}
Material& Material::operator= (const Material& material) {
QMutexLocker locker(&_textureMapsMutex);
_key = (material._key);
_textureMaps = (material._textureMaps);
_hasCalculatedTextureInfo = false;
// copied: create the Buffer to store the properties, avoid holding a ref to the old Buffer
Schema schema;
@ -112,6 +115,8 @@ void Material::setScattering(float scattering) {
}
void Material::setTextureMap(MapChannel channel, const TextureMapPointer& textureMap) {
QMutexLocker locker(&_textureMapsMutex);
if (textureMap) {
_key.setMapChannel(channel, (true));
_textureMaps[channel] = textureMap;
@ -119,6 +124,7 @@ void Material::setTextureMap(MapChannel channel, const TextureMapPointer& textur
_key.setMapChannel(channel, (false));
_textureMaps.erase(channel);
}
_hasCalculatedTextureInfo = false;
_schemaBuffer.edit<Schema>()._key = (uint32)_key._flags.to_ulong();
@ -173,6 +179,8 @@ void Material::resetOpacityMap() const {
const TextureMapPointer Material::getTextureMap(MapChannel channel) const {
QMutexLocker locker(&_textureMapsMutex);
auto result = _textureMaps.find(channel);
if (result != _textureMaps.end()) {
return (result->second);
@ -180,3 +188,37 @@ const TextureMapPointer Material::getTextureMap(MapChannel channel) const {
return TextureMapPointer();
}
}
bool Material::calculateMaterialInfo() const {
if (!_hasCalculatedTextureInfo) {
QMutexLocker locker(&_textureMapsMutex);
bool allTextures = true; // assume we got this...
_textureSize = 0;
_textureCount = 0;
for (auto const &textureMapItem : _textureMaps) {
auto textureMap = textureMapItem.second;
if (textureMap) {
auto textureSoure = textureMap->getTextureSource();
if (textureSoure) {
auto texture = textureSoure->getGPUTexture();
if (texture) {
auto size = texture->getSize();
_textureSize += size;
_textureCount++;
} else {
allTextures = false;
}
} else {
allTextures = false;
}
} else {
allTextures = false;
}
}
_hasCalculatedTextureInfo = allTextures;
}
return _hasCalculatedTextureInfo;
}

View file

@ -11,6 +11,8 @@
#ifndef hifi_model_Material_h
#define hifi_model_Material_h
#include <QMutex>
#include <bitset>
#include <map>
@ -324,7 +326,7 @@ public:
// The texture map to channel association
void setTextureMap(MapChannel channel, const TextureMapPointer& textureMap);
const TextureMaps& getTextureMaps() const { return _textureMaps; }
const TextureMaps& getTextureMaps() const { return _textureMaps; } // FIXME - not thread safe...
const TextureMapPointer getTextureMap(MapChannel channel) const;
// Albedo maps cannot have opacity detected until they are loaded
@ -344,12 +346,25 @@ public:
};
const UniformBufferView& getTexMapArrayBuffer() const { return _texMapArrayBuffer; }
int getTextureCount() const { calculateMaterialInfo(); return _textureCount; }
size_t getTextureSize() const { calculateMaterialInfo(); return _textureSize; }
bool hasTextureInfo() const { return _hasCalculatedTextureInfo; }
private:
mutable MaterialKey _key;
mutable UniformBufferView _schemaBuffer;
mutable UniformBufferView _texMapArrayBuffer;
TextureMaps _textureMaps;
mutable QMutex _textureMapsMutex { QMutex::Recursive };
mutable size_t _textureSize { 0 };
mutable int _textureCount { 0 };
mutable bool _hasCalculatedTextureInfo { false };
bool calculateMaterialInfo() const;
};
typedef std::shared_ptr< Material > MaterialPointer;

View file

@ -71,39 +71,8 @@ void MeshPartPayload::updateTransform(const Transform& transform, const Transfor
void MeshPartPayload::updateMaterial(model::MaterialPointer drawMaterial) {
_drawMaterial = drawMaterial;
calculateMaterialSize();
}
bool MeshPartPayload::calculateMaterialSize() {
bool allTextures = true; // assume we got this...
_materialTextureSize = 0;
_materialTextureCount = 0;
auto textureMaps = _drawMaterial->getTextureMaps();
for (auto const &textureMapItem : textureMaps) {
auto textureMap = textureMapItem.second;
if (textureMap) {
auto textureSoure = textureMap->getTextureSource();
if (textureSoure) {
auto texture = textureSoure->getGPUTexture();
if (texture) {
//auto storedSize = texture->getStoredSize();
auto size = texture->getSize();
_materialTextureSize += size;
_materialTextureCount++;
} else {
allTextures = false;
}
} else {
allTextures = false;
}
} else {
allTextures = false;
}
}
return allTextures;
}
ItemKey MeshPartPayload::getKey() const {
ItemKey::Builder builder;
builder.withTypeShape();
@ -378,7 +347,6 @@ void ModelMeshPartPayload::initCache() {
auto networkMaterial = _model->getGeometry()->getShapeMaterial(_shapeID);
if (networkMaterial) {
_drawMaterial = networkMaterial;
calculateMaterialSize();
}
}

View file

@ -66,13 +66,9 @@ public:
bool _hasColorAttrib = false;
size_t getVerticesCount() const { return _drawMesh ? _drawMesh->getNumVertices() : 0; }
size_t getMaterialTextureSize() { return _materialTextureSize; }
int getMaterialTextureCount() { return _materialTextureCount; }
bool calculateMaterialSize();
protected:
size_t _materialTextureSize { 0 };
int _materialTextureCount { 0 };
size_t getMaterialTextureSize() { return _drawMaterial ? _drawMaterial->getTextureSize() : 0; }
int getMaterialTextureCount() { return _drawMaterial ? _drawMaterial->getTextureCount() : 0; }
bool hasTextureInfo() const { return _drawMaterial ? _drawMaterial->hasTextureInfo() : false; }
};
namespace render {

View file

@ -168,10 +168,9 @@ void Model::calculateTextureInfo() {
bool allTexturesLoaded = true;
foreach(auto renderItem, _modelMeshRenderItemsSet) {
auto meshPart = renderItem.get();
bool allTexturesForThisMesh = meshPart->calculateMaterialSize();
allTexturesLoaded = allTexturesLoaded & allTexturesForThisMesh;
textureSize += meshPart->getMaterialTextureSize();
textureCount += meshPart->getMaterialTextureCount();
allTexturesLoaded = allTexturesLoaded & meshPart->hasTextureInfo();
}
_renderInfoTextureSize = textureSize;
_renderInfoTextureCount = textureCount;