adding locking to updateGeometry method to prevent race conditions

This commit is contained in:
ericrius1 2015-06-30 15:01:19 -07:00
parent 9fd30581d1
commit 0131a502cc
4 changed files with 41 additions and 27 deletions

View file

@ -23,6 +23,8 @@
EntityItemPointer RenderableQuadEntityItem::factory(const EntityItemID& entityID, const EntityItemProperties& properties) {
return EntityItemPointer(new RenderableQuadEntityItem(entityID, properties));
}
@ -70,25 +72,28 @@ int generateColor() {
}
void RenderableQuadEntityItem::updateGeometry() {
if (_pointsChanged) {
int compactColor = generateColor();
_numVertices = 0;
_verticesBuffer.reset(new gpu::Buffer());
int vertexIndex = 0;
for (int i = 0; i < _normals.size(); i++) {
compactColor = generateColor();
_verticesBuffer->append(sizeof(glm::vec3), (const gpu::Byte*)&_vertices.at(vertexIndex++));
_verticesBuffer->append(sizeof(glm::vec3), (const gpu::Byte*)&_vertices.at(vertexIndex));
vertexIndex++;
_verticesBuffer->append(sizeof(glm::vec3), (const gpu::Byte*)&_normals.at(i));
_verticesBuffer->append(sizeof(int), (gpu::Byte*)&compactColor);
_verticesBuffer->append(sizeof(glm::vec3), (const gpu::Byte*)&_vertices.at(vertexIndex++));
_verticesBuffer->append(sizeof(glm::vec3), (const gpu::Byte*)&_vertices.at(vertexIndex));
vertexIndex++;
_verticesBuffer->append(sizeof(glm::vec3), (const gpu::Byte*)&_normals.at(i));
_verticesBuffer->append(sizeof(int), (gpu::Byte*)&compactColor);
_numVertices +=2;
}
_pointsChanged = false;
}
}
@ -102,12 +107,14 @@ void RenderableQuadEntityItem::render(RenderArgs* args) {
createPipeline();
}
PerformanceTimer perfTimer("RenderableQuadEntityItem::render");
Q_ASSERT(getType() == EntityTypes::Quad);
Q_ASSERT(args->_batch);
if (_pointsChanged) {
updateGeometry();
}
gpu::Batch& batch = *args->_batch;
Transform transform = Transform();

View file

@ -17,6 +17,7 @@
#include "RenderableDebugableEntityItem.h"
#include "RenderableEntityItem.h"
#include <GeometryCache.h>
#include <QReadWriteLock>
class RenderableQuadEntityItem : public QuadEntityItem {
public:
@ -35,6 +36,7 @@ protected:
void updateGeometry();
gpu::BufferPointer _verticesBuffer;
unsigned int _numVertices;
};

View file

@ -44,7 +44,7 @@ _vertices(QVector<glm::vec3>(0))
}
EntityItemProperties QuadEntityItem::getProperties() const {
_quadReadWriteLock.lockForWrite();
EntityItemProperties properties = EntityItem::getProperties(); // get the properties from our base class
@ -59,11 +59,12 @@ EntityItemProperties QuadEntityItem::getProperties() const {
properties._glowLevel = getGlowLevel();
properties._glowLevelChanged = false;
_quadReadWriteLock.unlock();
return properties;
}
bool QuadEntityItem::setProperties(const EntityItemProperties& properties) {
_quadReadWriteLock.lockForWrite();
bool somethingChanged = false;
somethingChanged = EntityItem::setProperties(properties); // set the properties in our base class
@ -83,7 +84,10 @@ bool QuadEntityItem::setProperties(const EntityItemProperties& properties) {
}
setLastEdited(properties._lastEdited);
}
_quadReadWriteLock.unlock();
return somethingChanged;
}
bool QuadEntityItem::appendPoint(const glm::vec3& point) {
@ -106,7 +110,6 @@ bool QuadEntityItem::setNormals(const QVector<glm::vec3> &normals) {
return false;
}
_normals = normals;
qDebug() << "NORMALS: " << normals;
_vertices.clear();
//Go through and create vertices for triangle strip based on normals
if (_normals.size() != _points.size()) {
@ -114,10 +117,11 @@ bool QuadEntityItem::setNormals(const QVector<glm::vec3> &normals) {
}
glm::vec3 v1, v2, tangent, binormal, point;
for (int i = 0; i < _points.size()-1; i++) {
float width = (static_cast <float> (rand()) / static_cast <float> (RAND_MAX) * .1) + .02;
point = _points.at(i);
//Get tangent
tangent = _points.at(i+1) - point;
binormal = glm::normalize(glm::cross(tangent, normals.at(i))) * _lineWidth;
binormal = glm::normalize(glm::cross(tangent, normals.at(i))) * width;
v1 = point + binormal;
v2 = point - binormal;
_vertices << v1 << v2;
@ -172,7 +176,7 @@ bool QuadEntityItem::setLinePoints(const QVector<glm::vec3>& points) {
int QuadEntityItem::readEntitySubclassDataFromBuffer(const unsigned char* data, int bytesLeftToRead,
ReadBitstreamToTreeParams& args,
EntityPropertyFlags& propertyFlags, bool overwriteLocalData) {
_quadReadWriteLock.lockForWrite();
int bytesRead = 0;
const unsigned char* dataAt = data;
@ -181,7 +185,7 @@ int QuadEntityItem::readEntitySubclassDataFromBuffer(const unsigned char* data,
READ_ENTITY_PROPERTY(PROP_LINE_POINTS, QVector<glm::vec3>, setLinePoints);
READ_ENTITY_PROPERTY(PROP_NORMALS, QVector<glm::vec3>, setNormals);
_quadReadWriteLock.unlock();
return bytesRead;
}

View file

@ -81,6 +81,7 @@ class QuadEntityItem : public EntityItem {
QVector<glm::vec3> _points;
QVector<glm::vec3> _vertices;
QVector<glm::vec3> _normals;
mutable QReadWriteLock _quadReadWriteLock;
};
#endif // hifi_QuadEntityItem_h