Fix mesh render update blocking main UI

master
Jeremy Hu 2018-09-18 15:20:51 +08:00
parent 1ab3d16bd4
commit d3e28e88f2
2 changed files with 24 additions and 8 deletions

View File

@ -10,9 +10,10 @@
ModelMeshBinder::ModelMeshBinder() : ModelMeshBinder::ModelMeshBinder() :
m_mesh(nullptr), m_mesh(nullptr),
m_newMesh(nullptr),
m_renderTriangleVertexCount(0), m_renderTriangleVertexCount(0),
m_renderEdgeVertexCount(0), m_renderEdgeVertexCount(0),
m_meshUpdated(false), m_newMeshComing(false),
m_showWireframes(false), m_showWireframes(false),
m_hasTexture(false), m_hasTexture(false),
m_texture(nullptr) m_texture(nullptr)
@ -22,16 +23,17 @@ ModelMeshBinder::ModelMeshBinder() :
ModelMeshBinder::~ModelMeshBinder() ModelMeshBinder::~ModelMeshBinder()
{ {
delete m_mesh; delete m_mesh;
delete m_newMesh;
delete m_texture; delete m_texture;
} }
void ModelMeshBinder::updateMesh(MeshLoader *mesh) void ModelMeshBinder::updateMesh(MeshLoader *mesh)
{ {
QMutexLocker lock(&m_meshMutex); QMutexLocker lock(&m_newMeshMutex);
if (mesh != m_mesh) { if (mesh != m_mesh) {
delete m_mesh; delete m_newMesh;
m_mesh = mesh; m_newMesh = mesh;
m_meshUpdated = true; m_newMeshComing = true;
} }
} }
@ -114,9 +116,22 @@ void ModelMeshBinder::initialize()
void ModelMeshBinder::paint(ModelShaderProgram *program) void ModelMeshBinder::paint(ModelShaderProgram *program)
{ {
MeshLoader *newMesh = nullptr;
bool hasNewMesh = false;
if (m_newMeshComing) {
QMutexLocker lock(&m_newMeshMutex);
if (m_newMeshComing) {
newMesh = m_newMesh;
m_newMesh = nullptr;
m_newMeshComing = false;
hasNewMesh = true;
}
}
{ {
QMutexLocker lock(&m_meshMutex); QMutexLocker lock(&m_meshMutex);
if (m_meshUpdated) { if (hasNewMesh) {
delete m_mesh;
m_mesh = newMesh;
if (m_mesh) { if (m_mesh) {
m_hasTexture = nullptr != m_mesh->textureImage(); m_hasTexture = nullptr != m_mesh->textureImage();
delete m_texture; delete m_texture;
@ -166,7 +181,6 @@ void ModelMeshBinder::paint(ModelShaderProgram *program)
m_renderTriangleVertexCount = 0; m_renderTriangleVertexCount = 0;
m_renderEdgeVertexCount = 0; m_renderEdgeVertexCount = 0;
} }
m_meshUpdated = false;
} }
} }

View File

@ -24,9 +24,10 @@ public:
bool isWireframesVisible(); bool isWireframesVisible();
private: private:
MeshLoader *m_mesh; MeshLoader *m_mesh;
MeshLoader *m_newMesh;
int m_renderTriangleVertexCount; int m_renderTriangleVertexCount;
int m_renderEdgeVertexCount; int m_renderEdgeVertexCount;
bool m_meshUpdated; bool m_newMeshComing;
bool m_showWireframes; bool m_showWireframes;
bool m_hasTexture; bool m_hasTexture;
QOpenGLTexture *m_texture; QOpenGLTexture *m_texture;
@ -36,6 +37,7 @@ private:
QOpenGLVertexArrayObject m_vaoEdge; QOpenGLVertexArrayObject m_vaoEdge;
QOpenGLBuffer m_vboEdge; QOpenGLBuffer m_vboEdge;
QMutex m_meshMutex; QMutex m_meshMutex;
QMutex m_newMeshMutex;
}; };
#endif #endif