From 93866c8a742d5b6c1c612c17b71f2e5e57fe6b74 Mon Sep 17 00:00:00 2001 From: Sean Harmer Date: Sun, 10 Sep 2017 11:23:46 +0100 Subject: Don't send change notifications for unneeded properties The backend doesn't need to know about the rotation angles used to update the transform. Saves about 3% of time on the main thread related to mallocing the property change notifications. Change-Id: I784baf705cdd697ba860dc7a0c2e0e1c9ee467ec Reviewed-by: Paul Lemire --- tests/manual/bigscene-cpp/entity.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'tests') diff --git a/tests/manual/bigscene-cpp/entity.cpp b/tests/manual/bigscene-cpp/entity.cpp index 9a4ecae3b..a4625a9b2 100644 --- a/tests/manual/bigscene-cpp/entity.cpp +++ b/tests/manual/bigscene-cpp/entity.cpp @@ -106,7 +106,9 @@ void Entity::setTheta(float theta) return; m_theta = theta; + const bool wasBlocked = blockNotifications(true); emit thetaChanged(theta); + blockNotifications(wasBlocked); updateTransform(); } @@ -116,7 +118,9 @@ void Entity::setPhi(float phi) return; m_phi = phi; + const bool wasBlocked = blockNotifications(true); emit phiChanged(phi); + blockNotifications(wasBlocked); updateTransform(); } -- cgit v1.2.3 From 733afa3b259f78b81145ed2d1a8b8cc54184c4aa Mon Sep 17 00:00:00 2001 From: Sean Harmer Date: Sun, 10 Sep 2017 11:27:50 +0100 Subject: Use qFuzzyCompare for rotation angles Fixes clang warning. Change-Id: I1831bd112c33cdd1fe9cc7ae169cfba05cb8a694 Reviewed-by: Paul Lemire --- tests/manual/bigscene-cpp/entity.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/manual/bigscene-cpp/entity.cpp b/tests/manual/bigscene-cpp/entity.cpp index a4625a9b2..c3786275e 100644 --- a/tests/manual/bigscene-cpp/entity.cpp +++ b/tests/manual/bigscene-cpp/entity.cpp @@ -102,7 +102,7 @@ QColor Entity::diffuseColor() const void Entity::setTheta(float theta) { - if (m_theta == theta) + if (qFuzzyCompare(m_theta, theta)) return; m_theta = theta; @@ -114,7 +114,7 @@ void Entity::setTheta(float theta) void Entity::setPhi(float phi) { - if (m_phi == phi) + if (qFuzzyCompare(m_phi, phi)) return; m_phi = phi; -- cgit v1.2.3 From 4f2ff902545842bc715a936ff0c70b3ca3ce0cc9 Mon Sep 17 00:00:00 2001 From: Sean Harmer Date: Sun, 10 Sep 2017 11:49:46 +0100 Subject: Share the mesh in bigscene-cpp No point loading the same data 1k times. Shaves 35% off the time from application start to first frame being complete. Change-Id: I8cd7c8a5aa377c0663c03e35d7a4c03f50b052f6 Reviewed-by: Paul Lemire --- tests/manual/bigscene-cpp/entity.cpp | 7 ------- tests/manual/bigscene-cpp/entity.h | 5 ++++- tests/manual/bigscene-cpp/main.cpp | 8 ++++++++ 3 files changed, 12 insertions(+), 8 deletions(-) (limited to 'tests') diff --git a/tests/manual/bigscene-cpp/entity.cpp b/tests/manual/bigscene-cpp/entity.cpp index c3786275e..6cd969777 100644 --- a/tests/manual/bigscene-cpp/entity.cpp +++ b/tests/manual/bigscene-cpp/entity.cpp @@ -58,15 +58,8 @@ Entity::Entity(Qt3DCore::QNode *parent) : QEntity(parent) , m_transform(new Qt3DCore::QTransform()) - , m_mesh(new Qt3DExtras::QCylinderMesh()) , m_material(new Qt3DExtras::QPhongMaterial()) { - m_mesh->setRings(50.0f); - m_mesh->setSlices(30.0f); - m_mesh->setRadius(2.5f); - m_mesh->setLength(5.0f); - - addComponent(m_mesh); addComponent(m_transform); addComponent(m_material); } diff --git a/tests/manual/bigscene-cpp/entity.h b/tests/manual/bigscene-cpp/entity.h index 04dcbbcb3..6c2310a35 100644 --- a/tests/manual/bigscene-cpp/entity.h +++ b/tests/manual/bigscene-cpp/entity.h @@ -61,6 +61,10 @@ namespace Qt3DCore { class QTransform; } +namespace Qt3DRender { +class QGeometryRenderer; +} + namespace Qt3DExtras { class QCylinderMesh; class QPhongMaterial; @@ -101,7 +105,6 @@ private: private: Qt3DCore::QTransform *m_transform; - Qt3DExtras::QCylinderMesh *m_mesh; Qt3DExtras::QPhongMaterial *m_material; float m_theta; float m_phi; diff --git a/tests/manual/bigscene-cpp/main.cpp b/tests/manual/bigscene-cpp/main.cpp index 791aeb19d..cf893e051 100644 --- a/tests/manual/bigscene-cpp/main.cpp +++ b/tests/manual/bigscene-cpp/main.cpp @@ -81,6 +81,13 @@ int main(int ac, char **av) QEntity *root = new QEntity(); + // Mesh + auto *mesh = new Qt3DExtras::QCylinderMesh(root); + mesh->setRings(50.0f); + mesh->setSlices(30.0f); + mesh->setRadius(2.5f); + mesh->setLength(5.0f); + // Camera QCamera *cameraEntity = view.camera(); cameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f); @@ -99,6 +106,7 @@ int main(int ac, char **av) // Scene for (int i = 0; i < max; i++) { Entity *e = new Entity(); + e->addComponent(mesh); const float angle = M_PI * 2.0f * i * det * 10.; e->setDiffuseColor(QColor(qFabs(qCos(angle)) * 255, 204, 75)); -- cgit v1.2.3 From 054ecec467076dbe4e82db27dbc66a5876d1b199 Mon Sep 17 00:00:00 2001 From: Sean Harmer Date: Sun, 10 Sep 2017 12:17:32 +0100 Subject: Share the effect from the phong material This only loads the shader source once instead of 1000 times. Reduces the time to first frame by a further 30% by massively reducing the work the main thread has to do at startup. The Qt3DExtras material classes are convenient but lead to bad practices like this. We should provide a way to allow them to share the effect easily but still use the provided materials for their API. Perhaps a new ctor overload that takes a QEffect pointer would work nicely. Also fixed some float vs double vs int precision warnings as a drive by. Change-Id: I181de8d95841eb88c91c29ef6f45d215da079b98 Reviewed-by: Paul Lemire --- tests/manual/bigscene-cpp/entity.cpp | 20 ++++++++++++++------ tests/manual/bigscene-cpp/entity.h | 13 ++++++------- tests/manual/bigscene-cpp/main.cpp | 20 ++++++++++++-------- 3 files changed, 32 insertions(+), 21 deletions(-) (limited to 'tests') diff --git a/tests/manual/bigscene-cpp/entity.cpp b/tests/manual/bigscene-cpp/entity.cpp index 6cd969777..876a2d171 100644 --- a/tests/manual/bigscene-cpp/entity.cpp +++ b/tests/manual/bigscene-cpp/entity.cpp @@ -51,15 +51,21 @@ #include "entity.h" #include -#include +#include +#include #include #include -Entity::Entity(Qt3DCore::QNode *parent) +Entity::Entity(Qt3DRender::QEffect *effect, Qt3DCore::QNode *parent) : QEntity(parent) , m_transform(new Qt3DCore::QTransform()) - , m_material(new Qt3DExtras::QPhongMaterial()) + , m_material(new Qt3DRender::QMaterial()) + , m_diffuseColorParam(new Qt3DRender::QParameter()) { + m_diffuseColorParam->setName(QLatin1String("kd")); + m_material->addParameter(m_diffuseColorParam); + m_material->setEffect(effect); + addComponent(m_transform); addComponent(m_material); } @@ -90,7 +96,7 @@ QVector3D Entity::position() const QColor Entity::diffuseColor() const { - return m_material->diffuse(); + return m_diffuseColorParam->value().value(); } void Entity::setTheta(float theta) @@ -129,9 +135,11 @@ void Entity::setPosition(QVector3D position) void Entity::setDiffuseColor(QColor diffuseColor) { - if (m_material->diffuse() == diffuseColor) + if (m_diffuseColorParam->value().value() == diffuseColor) return; - m_material->setDiffuse(diffuseColor); + m_diffuseColorParam->setValue(QVariant::fromValue(diffuseColor)); + const bool wasBlocked = blockNotifications(true); emit diffuseColorChanged(diffuseColor); + blockNotifications(wasBlocked); } diff --git a/tests/manual/bigscene-cpp/entity.h b/tests/manual/bigscene-cpp/entity.h index 6c2310a35..656511cdc 100644 --- a/tests/manual/bigscene-cpp/entity.h +++ b/tests/manual/bigscene-cpp/entity.h @@ -62,12 +62,10 @@ class QTransform; } namespace Qt3DRender { +class QEffect; class QGeometryRenderer; -} - -namespace Qt3DExtras { -class QCylinderMesh; -class QPhongMaterial; +class QMaterial; +class QParameter; } QT_END_NAMESPACE @@ -81,7 +79,7 @@ class Entity : public Qt3DCore::QEntity Q_PROPERTY(QColor diffuseColor READ diffuseColor WRITE setDiffuseColor NOTIFY diffuseColorChanged) public: - Entity(Qt3DCore::QNode *parent = 0); + Entity(Qt3DRender::QEffect *effect, Qt3DCore::QNode *parent = 0); float theta() const; float phi() const; @@ -105,7 +103,8 @@ private: private: Qt3DCore::QTransform *m_transform; - Qt3DExtras::QPhongMaterial *m_material; + Qt3DRender::QMaterial *m_material; + Qt3DRender::QParameter *m_diffuseColorParam; float m_theta; float m_phi; QVector3D m_position; diff --git a/tests/manual/bigscene-cpp/main.cpp b/tests/manual/bigscene-cpp/main.cpp index cf893e051..ef296e04d 100644 --- a/tests/manual/bigscene-cpp/main.cpp +++ b/tests/manual/bigscene-cpp/main.cpp @@ -88,6 +88,10 @@ int main(int ac, char **av) mesh->setRadius(2.5f); mesh->setLength(5.0f); + // Material + auto phongMaterial = new Qt3DExtras::QPhongMaterial(root); + auto effect = phongMaterial->effect(); + // Camera QCamera *cameraEntity = view.camera(); cameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f); @@ -99,18 +103,20 @@ int main(int ac, char **av) Qt3DExtras::QFirstPersonCameraController *camController = new Qt3DExtras::QFirstPersonCameraController(root); camController->setCamera(cameraEntity); - const float radius = 100.0f; + const double radius = 100.0; const int max = 1000; - const float det = 1.0f / max; + const double det = 1.0 / max; // Scene for (int i = 0; i < max; i++) { - Entity *e = new Entity(); + Entity *e = new Entity(effect, root); e->addComponent(mesh); - const float angle = M_PI * 2.0f * i * det * 10.; + const double angle = M_PI * 2.0 * double(i) * det * 10.; - e->setDiffuseColor(QColor(qFabs(qCos(angle)) * 255, 204, 75)); - e->setPosition(QVector3D(radius * qCos(angle), -200.* i * det, radius * qSin(angle))); + e->setDiffuseColor(QColor(int(qFabs(qCos(angle)) * 255.0), 204, 75)); + e->setPosition(QVector3D(float(radius * qCos(angle)), + float(-200.0 * i * det), + float(radius * qSin(angle)))); e->setTheta(30.0f * i); e->setPhi(45.0f * i); @@ -127,8 +133,6 @@ int main(int ac, char **av) animZ->setEndValue(QVariant::fromValue((i + 1) * 380.0f)); animZ->setLoopCount(-1); animZ->start(); - - e->setParent(root); } view.setRootEntity(root); -- cgit v1.2.3