From d3e28e88f23165241895b816584a4bf7d9a6706f Mon Sep 17 00:00:00 2001 From: Jeremy Hu Date: Tue, 18 Sep 2018 15:20:51 +0800 Subject: [PATCH] Fix mesh render update blocking main UI --- src/modelmeshbinder.cpp | 28 +++++++++++++++++++++------- src/modelmeshbinder.h | 4 +++- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/modelmeshbinder.cpp b/src/modelmeshbinder.cpp index 7277f85c..2b025ac6 100644 --- a/src/modelmeshbinder.cpp +++ b/src/modelmeshbinder.cpp @@ -10,9 +10,10 @@ ModelMeshBinder::ModelMeshBinder() : m_mesh(nullptr), + m_newMesh(nullptr), m_renderTriangleVertexCount(0), m_renderEdgeVertexCount(0), - m_meshUpdated(false), + m_newMeshComing(false), m_showWireframes(false), m_hasTexture(false), m_texture(nullptr) @@ -22,16 +23,17 @@ ModelMeshBinder::ModelMeshBinder() : ModelMeshBinder::~ModelMeshBinder() { delete m_mesh; + delete m_newMesh; delete m_texture; } void ModelMeshBinder::updateMesh(MeshLoader *mesh) { - QMutexLocker lock(&m_meshMutex); + QMutexLocker lock(&m_newMeshMutex); if (mesh != m_mesh) { - delete m_mesh; - m_mesh = mesh; - m_meshUpdated = true; + delete m_newMesh; + m_newMesh = mesh; + m_newMeshComing = true; } } @@ -114,9 +116,22 @@ void ModelMeshBinder::initialize() 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); - if (m_meshUpdated) { + if (hasNewMesh) { + delete m_mesh; + m_mesh = newMesh; if (m_mesh) { m_hasTexture = nullptr != m_mesh->textureImage(); delete m_texture; @@ -166,7 +181,6 @@ void ModelMeshBinder::paint(ModelShaderProgram *program) m_renderTriangleVertexCount = 0; m_renderEdgeVertexCount = 0; } - m_meshUpdated = false; } } diff --git a/src/modelmeshbinder.h b/src/modelmeshbinder.h index 9505769d..721ed085 100644 --- a/src/modelmeshbinder.h +++ b/src/modelmeshbinder.h @@ -24,9 +24,10 @@ public: bool isWireframesVisible(); private: MeshLoader *m_mesh; + MeshLoader *m_newMesh; int m_renderTriangleVertexCount; int m_renderEdgeVertexCount; - bool m_meshUpdated; + bool m_newMeshComing; bool m_showWireframes; bool m_hasTexture; QOpenGLTexture *m_texture; @@ -36,6 +37,7 @@ private: QOpenGLVertexArrayObject m_vaoEdge; QOpenGLBuffer m_vboEdge; QMutex m_meshMutex; + QMutex m_newMeshMutex; }; #endif